无法使用嵌套for-each循环n XSLT生成输出

时间:2016-11-24 10:14:08

标签: html json xml xslt

我试图将Json中的数据表示为XSLT。为此,我将Json转换为XML,然后在XSLT中使用该XML。我想循环到多个具有多个数字和地址的联系人。我试过这个,但似乎我的每个循环的嵌套都不能正常工作。

这是我的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

1 个答案:

答案 0 :(得分: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