2016-11-24 48 views
1

我試圖將Json中的數據表示爲XSLT。爲此,我將Json轉換爲XML,然後在XSLT中使用該XML。我想循環到具有多個號碼和地址的多個聯繫人。我試過這個,但似乎我爲每個循環嵌套不能正常工作。無法使用嵌套for-each循環生成輸出n XSLT

這裏是我的Json

{"checkForDuplicateRes": { 
    "resHdr": { 
     "consumerContext":  { 
     "applicationId": "CRM", 
     "terminalId": "12345" 
     }, 
     "serviceContext":  { 
     "uniqueMsgId": "111120161124123542", 
     "reqMsgDateTime": "2016-11-24T12:35:42.4096843+05:30", 
     "timeZone": "2016-11-24T12:35:42.4096843+05:30", 
     "serviceName": "checkForDuplicate", 
     "serviceVersion": "1", 
     "conversationId": "21212112", 
     "parentMsgId": " 31321212 " 
     }, 
     "providerContext":  { 
     "providerId": "POSIDEX", 
     "responseMsgDateTime": "2016-11-24T12:35:42.4096843+05:30" 
     }, 
     "responseStatus":  { 
     "status": "0", 
     "esbResDateTime": "2016-11-24 12:35:07.379973" 
     } 
    }, 
    "body": { 
     "requestId": "111120161124123542", 
     "status": "S", 
     "matchCount": "1", 
     "message": "Process Completed", 
     "matchType": "AADHAR_NO,NAME,VOTERID", 
     "sourceId": "0", 
     "customerDtls": [  { 
     "custId": "1102000007", 
     "name": "M PADMA", 
     "dateOfBirth": "01/01/1974", 
     "gender": "Female", 
     "motherName": "AKKAMMA", 
     "fatherName": "MARAPPA", 
     "martialStatus": "Married", 
     "spouse": "RAJU", 
     "aadhar": "578332434530", 
     "voterId": "ZLW4619946", 
     "ContactDtls": [{"mobileNumber": "9482004675"}], 
     "addressDtls":   [ 
         { 
       "addresstype": "Residential Address", 
       "address": "480, RAJENDRANAGARA KORAMANGALA VIVEK NAGARB POST BANGALORE VIVEKNAGAR BANGALORE KARNATAKA, Bangalore, KARNATAKA", 
       "city": "Bangalore", 
       "state": "KARNATAKA", 
       "pincode": "560047" 
      }, 
         { 
       "addresstype": "Permanent Address", 
       "address": "RUDDRA BOOMI INSIDE, WILSON GARDEN BANGALORE, Karnataka", 
       "city": "BANGALORE" 
      }, 
         { 
       "addresstype": "Business Address", 
       "address": "NO.222,13TH CROSS, AMBEDKAR NAGAR, KORAMANGALA,", 
       "city": "BANGALORE", 
       "pincode": "560047" 
      } 
     ] 
     }] 
    } 
}} 

我轉換XML。

<?xml version="1.0" encoding="UTF-8" ?> 
<checkForDuplicateRes> 
    <resHdr> 
    <consumerContext> 
     <applicationId>CRM</applicationId> 
     <terminalId>12345</terminalId> 
    </consumerContext> 
    <serviceContext> 
     <uniqueMsgId>111120161124123542</uniqueMsgId> 
     <reqMsgDateTime>2016-11-24T12:35:42.4096843+05:30</reqMsgDateTime> 
     <timeZone>2016-11-24T12:35:42.4096843+05:30</timeZone> 
     <serviceName>checkForDuplicate</serviceName> 
     <serviceVersion>1</serviceVersion> 
     <conversationId>21212112</conversationId> 
     <parentMsgId> 31321212 </parentMsgId> 
    </serviceContext> 
    <providerContext> 
     <providerId>POSIDEX</providerId> 
     <responseMsgDateTime>2016-11-24T12:35:42.4096843+05:30</responseMsgDateTime> 
    </providerContext> 
    <responseStatus> 
     <status>0</status> 
     <esbResDateTime>2016-11-24 12:35:07.379973</esbResDateTime> 
    </responseStatus> 
    </resHdr> 
    <body> 
    <requestId>111120161124123542</requestId> 
    <status>S</status> 
    <matchCount>1</matchCount> 
    <message>Process Completed</message> 
    <matchType>AADHAR_NO,NAME,VOTERID</matchType> 
    <sourceId>0</sourceId> 
    <customerDtls> 
     <custId>1102000007</custId> 
     <name>M PADMA</name> 
     <dateOfBirth>01/01/1974</dateOfBirth> 
     <gender>Female</gender> 
     <motherName>AKKAMMA</motherName> 
     <fatherName>MARAPPA</fatherName> 
     <martialStatus>Married</martialStatus> 
     <spouse>RAJU</spouse> 
     <aadhar>578332434530</aadhar> 
     <voterId>ZLW4619946</voterId> 
     <ContactDtls> 
     <mobileNumber>9482004675</mobileNumber> 
     </ContactDtls> 
     <addressDtls> 
     <addresstype>Residential Address</addresstype> 
     <address>480, RAJENDRANAGARA KORAMANGALA VIVEK NAGARB POST BANGALORE VIVEKNAGAR BANGALORE KARNATAKA, Bangalore, KARNATAKA</address> 
     <city>Bangalore</city> 
     <state>KARNATAKA</state> 
     <pincode>560047</pincode> 
     </addressDtls> 
     <addressDtls> 
     <addresstype>Permanent Address</addresstype> 
     <address>RUDDRA BOOMI INSIDE, WILSON GARDEN BANGALORE, Karnataka</address> 
     <city>BANGALORE</city> 
     </addressDtls> 
     <addressDtls> 
     <addresstype>Business Address</addresstype> 
     <address>NO.222,13TH CROSS, AMBEDKAR NAGAR, KORAMANGALA,</address> 
     <city>BANGALORE</city> 
     <pincode>560047</pincode> 
     </addressDtls> 
    </customerDtls> 
    </body> 
</checkForDuplicateRes> 

和XSLT我使用如下

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="html" encoding="utf-8" indent="no"/>     
    <xsl:template match="/"> 
     <html> 
      <head> 
       <style> 
.Dedupe{ 
    border-collapse: collapse; 
    width: 100%; 
    font-size: 11px; 
    font-weight: 400; 
word-break: normal; 
} 
.Dedupe th { 
    padding: 5px; 
    text-align: center; 
    border-bottom: 1px solid black; 
    background-color:#CDCDCD; 
    font-weight: 600 !important; 
} 

.Dedupe td { 
    padding: 5px !important; 
    text-align: center; 
    border-bottom: 1px solid black !important; 
} 
.Dedupe tr{ 
    color: #000; 
    font-family: Verdana,Arial,Helvetica,sans-serif; 
    font-size: 11px; 
    font-weight: 400; 
} 
.Dedupe tr:hover{background-color:#f5f5f5 !important} 
       </style> 
      </head> 
      <body> 
       <div style="width:120%; height:150px;overflow:auto;"> 
        <table border="1" class="Dedupe" > 
         <xsl:if test="checkForDuplicateRes/body/customerDtls"> 
          <tr> 
           <th rowspan="3">requestId</th> 
           <th rowspan="3">status</th> 
           <th rowspan="3">matchCount</th> 
           <th rowspan="3">message</th> 
           <th rowspan="3">sourceId</th> 
           <th colspan="16">customerDtls</th> 
          </tr> 
          <tr> 
           <th rowspan="2">custId</th> 
           <th rowspan="2">name</th> 
           <th rowspan="2">dateOfBirth</th> 
           <th rowspan="2">gender</th> 
           <th rowspan="2">motherName</th> 
           <th rowspan="2">fatherName</th> 
           <th rowspan="2">martialStatus</th> 
           <th rowspan="2">spouse</th> 
           <th rowspan="2">aadhar</th> 
           <th rowspan="2">voterId</th> 
           <th>ContactDtls</th> 
           <th colspan="5">addressDtls</th> 
          </tr> 
          <tr> 
           <th>mobileNumber</th> 
           <th>addresstype</th> 
           <th>address</th> 
           <th>city</th> 
           <th>state</th> 
           <th>pincode</th> 
          </tr> 

          <tr> 
           <td><xsl:value-of select="checkForDuplicateRes/body/requestId"/></td> 
           <td><xsl:value-of select="checkForDuplicateRes/body/status"/></td> 
           <td><xsl:value-of select="checkForDuplicateRes/body/matchCount"/></td> 
           <td><xsl:value-of select="checkForDuplicateRes/body/message"/></td> 
           <td><xsl:value-of select="checkForDuplicateRes/body/sourceId"/></td> 
           <xsl:for-each select="checkForDuplicateRes/body/customerDtls"> 
            <td><xsl:value-of select="custId"/></td> 
            <td><xsl:value-of select="name"/></td> 
            <td><xsl:value-of select="dateOfBirth"/></td> 
            <td><xsl:value-of select="gender"/></td> 
            <td><xsl:value-of select="motherName"/></td> 
            <td><xsl:value-of select="fatherName"/></td> 
            <td><xsl:value-of select="martialStatus"/></td> 
            <td><xsl:value-of select="spouse"/></td> 
            <td><xsl:value-of select="aadhar"/></td> 
            <td><xsl:value-of select="voterId"/></td> 
            <xsl:for-each select="ContactDtls"> 
             <td><xsl:value-of select="mobileNumber"/></td> 
            </xsl:for-each> 
            <xsl:for-each select="addressDtls"> 
             <td><xsl:value-of select="addresstype"/></td> 
             <td><xsl:value-of select="address"/></td> 
             <td><xsl:value-of select="city"/></td> 
             <td><xsl:value-of select="state"/></td> 
             <td><xsl:value-of select="pincode"/></td> 
            </xsl:for-each> 
           </xsl:for-each> 
          </tr> 

         </xsl:if> 
        </table> 
       </div> 
      </body> 
     </html>     
    </xsl:template>      
</xsl:stylesheet> 

非常感謝您的任何幫助。

編輯

解析在上面的XSLT提到的XML之後,地址記錄在同一條線上來,而我想上顯示針對單個獨立行這些地址(即住宅,永久和商家地址)聯繫。

編輯2

很少人會因爲得到什麼,我曾在編輯提到困惑。我的意思是說單個聯繫人的多個地址詳情(一個在另一個下面)。像我在下面的圖像中表示的東西。此圖片僅供參考,僅作爲實際名稱和編號。的列將是不同的,比我在下面的圖片中提到的更多。

enter image description here

+0

什麼是你的問題?更好的是,你期望的輸出是什麼? – Parfait

+0

@Parfait我編輯了我的問題。我希望它能清除你的疑惑。 –

回答

0

應考慮將樣式表分爲多個模板避免任何<xsl:for-each>電話。此外,直接找到多個<addressDtls>節點,再看看上面從環境獲取的祖先(<body><customerDtls>)值:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="html" encoding="utf-8" indent="no"/> 

    <xsl:template match="/checkForDuplicateRes"> 
     <html> 
      <head> 
       <style type="text/css"> 
        .Dedupe{ 
         border-collapse: collapse; 
         width: 100%; 
         font-size: 11px; 
         font-weight: 400; 
         word-break: normal; 
        } 
        .Dedupe th { 
         padding: 5px; 
         text-align: center; 
         border-bottom: 1px solid black; 
         background-color:#CDCDCD; 
         font-weight: 600 !important; 
        } 

        .Dedupe td { 
         padding: 5px !important; 
         text-align: center; 
         border-bottom: 1px solid black !important; 
        } 
        .Dedupe tr{ 
         color: #000; 
         font-family: Verdana,Arial,Helvetica,sans-serif; 
         font-size: 11px; 
         font-weight: 400; 
        } 
        .Dedupe tr:hover{background-color:#f5f5f5 !important} 
       </style> 
      </head>    
      <xsl:apply-templates select="body"/>    
     </html>    
    </xsl:template> 

    <xsl:template match="body"> 
     <xsl:copy> 
      <div style="width:120%; height:150px;overflow:auto;"> 
       <table border="1" class="Dedupe" > 
        <tr> 
         <th rowspan="3">requestId</th> 
         <th rowspan="3">status</th> 
         <th rowspan="3">matchCount</th> 
         <th rowspan="3">message</th> 
         <th rowspan="3">sourceId</th> 
         <th colspan="16">customerDtls</th> 
        </tr> 
        <tr> 
         <th rowspan="2">custId</th> 
         <th rowspan="2">name</th> 
         <th rowspan="2">dateOfBirth</th> 
         <th rowspan="2">gender</th> 
         <th rowspan="2">motherName</th> 
         <th rowspan="2">fatherName</th> 
         <th rowspan="2">martialStatus</th> 
         <th rowspan="2">spouse</th> 
         <th rowspan="2">aadhar</th> 
         <th rowspan="2">voterId</th> 
         <th>ContactDtls</th> 
         <th colspan="5">addressDtls</th> 
        </tr> 
        <tr> 
         <th>mobileNumber</th> 
         <th>addresstype</th> 
         <th>address</th> 
         <th>city</th> 
         <th>state</th> 
         <th>pincode</th> 
        </tr> 
        <xsl:apply-templates select="customerDtls"/>      
       </table>      
      </div> 
     </xsl:copy> 
    </xsl:template>  

    <xsl:template match="customerDtls"> 
     <xsl:apply-templates select="addressDtls"/> 
    </xsl:template> 

    <xsl:template match="addressDtls"> 
     <tr> 
      <td><xsl:value-of select="ancestor::body/requestId"/></td> 
      <td><xsl:value-of select="ancestor::body/status"/></td> 
      <td><xsl:value-of select="ancestor::body/matchCount"/></td> 
      <td><xsl:value-of select="ancestor::body/message"/></td> 
      <td><xsl:value-of select="ancestor::body/sourceId"/></td>                 
      <td><xsl:value-of select="ancestor::customerDtls/custId"/></td> 
      <td><xsl:value-of select="ancestor::customerDtls/name"/></td> 
      <td><xsl:value-of select="ancestor::customerDtls/dateOfBirth"/></td> 
      <td><xsl:value-of select="ancestor::customerDtls/gender"/></td> 
      <td><xsl:value-of select="ancestor::customerDtls/motherName"/></td> 
      <td><xsl:value-of select="ancestor::customerDtls/fatherName"/></td> 
      <td><xsl:value-of select="ancestor::customerDtls/martialStatus"/></td> 
      <td><xsl:value-of select="ancestor::customerDtls/spouse"/></td> 
      <td><xsl:value-of select="ancestor::customerDtls/aadhar"/></td> 
      <td><xsl:value-of select="ancestor::customerDtls/voterId"/></td>   
      <td><xsl:value-of select="ancestor::customerDtls/ContactDtls/mobileNumber"/></td> 
      <td><xsl:value-of select="addresstype"/></td> 
      <td><xsl:value-of select="address"/></td> 
      <td><xsl:value-of select="city"/></td> 
      <td><xsl:value-of select="state"/></td> 
      <td><xsl:value-of select="pincode"/></td>    
     </tr> 
    </xsl:template> 

</xsl:stylesheet> 

HTML Output

+0

感謝您的回答。由於我必須在同一行顯示單個聯繫人的多個地址詳細信息和聯繫人詳細信息,因此我使用表格標記調整了現有的XSLT。你可以在https://codeshare.io/5P3QE2找到它。它仍然沒有以正確的格式顯示數據,但我認爲它會起作用。 –

+0

你可以建議我從你身邊的任何改變,以提高相同。任何幫助,將不勝感激。 :) –

+0

同一行上的多個地址細節?這與你上面所說的相反:*而我想在單獨的行*上顯示這些地址(即住宅,永久和商業地址)。你真的需要什麼? – Parfait