简体   繁体   中英

how to select multiple tags with same name for nodes in xml for xslt?

I am new to Xslt. I am trying to use xslt to simplify a xml. But I am facing two issues:

1.) Same Tag names - I am not able to fetch the second tag name , only one gets fetched (PublishItem tag in the xml)

2.) There are multiple inner tags which are nested, having the same name which i am not able to fetch using xslt. (itemLife tag in xml)

This is the XSLT that I have created so far:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html> 
<body>
  <h2>Conversion</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th style="text-align:left">inventoryItemIndDD</th>
      <th style="text-align:left">variableWeightInd</th>
      <th style="text-align:left">itemDesc</th>
      <th style="text-align:left">itemName</th>
      <th style="text-align:left">environmentTypeCd</th>
      <th style="text-align:left">lineNum</th>
      <th style="text-align:left">itemHandlingLevelType</th>
<th style="text-align:left">Caseitem:inventoryItemIndDD</th>
      <th style="text-align:left">Caseitem:variableWeightInd</th>
      <th style="text-align:left">Caseitem:itemDesc</th>
      <th style="text-align:left">Caseitem:itemName</th>
      <th style="text-align:left">Caseitem:environmentTypeCd</th>
      <th style="text-align:left">Caseitem:lineNum</th>
      <th style="text-align:left">Caseitem:itemHandlingLevelType</th>
  <th style="text-align:left">Caseitem:itemLife1Days</th>
  <th style="text-align:left">Caseitem:itemLife2Days</th>
  <th style="text-align:left">Caseitem:itemLife3Days</th>

    </tr>

    <xsl:for-each select="/PublishItem/body/itemObject/item">
    <tr>
      <td><xsl:value-of select="inventoryItemIndDD"/></td>
      <td><xsl:value-of select="variableWeightInd"/></td>
      <td><xsl:value-of select="itemDescription/itemDesc"/></td>
      <td><xsl:value-of select="itemDescription/itemName"/></td>
      <td><xsl:value-of select="itemEnvironmentRecord/environmentType/environmentTypeCd"/></td>
      <td><xsl:value-of select="itemGroupMembership/itemGroup/itemGroupLine/lineNum"/></td>
      <td><xsl:value-of select="itemHandlingLevelType/itemHandlingLevelTypeCd"/></td>

<td><xsl:value-of select="itemHandlingStructure/parentItem/inventoryItemIndDD"/></td>
      <td><xsl:value-of select="itemHandlingStructure/parentItem/variableWeightInd"/></td>
      <td><xsl:value-of select="itemHandlingStructure/parentItem/itemDescription/itemDesc"/></td>
      <td><xsl:value-of select="itemHandlingStructure/parentItem/itemDescription/itemName"/></td>
      <td><xsl:value-of select="itemHandlingStructure/parentItem/itemEnvironmentRecord/environmentType/environmentTypeCd"/></td>
      <td><xsl:value-of select="itemHandlingStructure/parentItem/itemGroupMembership/itemGroup/itemGroupLine/lineNum"/></td>
      <td><xsl:value-of select="itemHandlingStructure/parentItem/itemHandlingLevelType/itemHandlingLevelTypeCd"/></td>
 <td><xsl:value-of select="itemHandlingStructure/parentItem/itemHandlingLevelType/itemLife/itemLifeDays"/></td> 
 <td><xsl:value-of select="itemHandlingStructure/parentItem/itemHandlingLevelType//itemLife/itemLifeDays"/></td>
 <td><xsl:value-of select="itemHandlingStructure/parentItem/itemHandlingLevelType//itemLife/itemLifeDays"/></td>
  <!-- <xsl:for-each select="itemHandlingStructure/parentItem/itemHandlingLevelType/itemLife"> -->
            <!-- <xsl:value-of select="."/> -->
       <!-- <xsl:if test="position() != last()">, </xsl:if> -->
        <!-- </xsl:for-each> -->
    </tr>
    </xsl:for-each>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Here is the XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<PublishItem xmlns:ebm="">
    <body>
        <itemObject>
            <item>
                <inventoryItemIndDD>1</inventoryItemIndDD>
                <variableWeightInd>0</variableWeightInd>
                <itemDescription>
                    <itemDesc>Gorgeous Green Smoothie mix</itemDesc>
                    <itemName>WR FRZ Gorgeous Green Smoothie Mix</itemName>
                </itemDescription>
                <itemEnvironmentRecord>
                    <environmentType>
                        <environmentTypeCd></environmentTypeCd>
                    </environmentType>
                </itemEnvironmentRecord>
                <itemGroupMembership>
                    <itemGroup>
                        <itemGroupLine>
                            <lineNum>793347</lineNum>
                        </itemGroupLine>
                    </itemGroup>
                </itemGroupMembership>
                <itemHandlingLevelType>
                    <itemHandlingLevelTypeCd>BASE</itemHandlingLevelTypeCd>
                </itemHandlingLevelType>
                <itemHandlingStructure>
                    <childItemQty>
                        <code>EACH</code>
                        <number>10.0</number>
                    </childItemQty>
                    <parentItem>
                        <inventoryItemIndDD>1</inventoryItemIndDD>
                        <variableWeightInd>0</variableWeightInd>
                        <itemDescription>
                            <itemDesc>Gorgeous Green Smoothie mix</itemDesc>
                            <itemName>WR FRZ Gorgeous Green Smoothie Test</itemName>
                        </itemDescription>
                        <itemEnvironmentRecord>
                            <environmentType>
                                <environmentTypeCd></environmentTypeCd>
                            </environmentType>
                        </itemEnvironmentRecord>
                        <itemGroupMembership>
                            <itemGroup>
                                <itemGroupLine>
                                    <lineNum>793347</lineNum>
                                </itemGroupLine>
                            </itemGroup>
                        </itemGroupMembership>
                        <itemHandlingLevelType>
                            <itemHandlingLevelTypeCd>BASE</itemHandlingLevelTypeCd>
                        </itemHandlingLevelType>
                        <itemLife>
                            <itemLifeDays>99</itemLifeDays>
                            <itemLifeType>
                                <itemLifeTypeCd></itemLifeTypeCd>
                            </itemLifeType>
                            <itemLifeTypeSymbol>
                                <itemLifeTypeSymbolCd></itemLifeTypeSymbolCd>
                            </itemLifeTypeSymbol>
                        </itemLife>
                        <itemLife>
                            <itemLifeDays>182</itemLifeDays>
                            <itemLifeType>
                                <itemLifeTypeCd></itemLifeTypeCd>
                            </itemLifeType>
                            <itemLifeTypeSymbol>
                                <itemLifeTypeSymbolCd></itemLifeTypeSymbolCd>
                            </itemLifeTypeSymbol>
                        </itemLife>
                        <itemLife>
                            <itemLifeDays>99</itemLifeDays>
                            <itemLifeType>
                                <itemLifeTypeCd></itemLifeTypeCd>
                            </itemLifeType>
                            <itemLifeTypeSymbol>
                                <itemLifeTypeSymbolCd></itemLifeTypeSymbolCd>
                            </itemLifeTypeSymbol>
                        </itemLife>
                        <itemStatusRecord>
                            <itemStatusCd></itemStatusCd>
                        </itemStatusRecord>                                             
                    </parentItem>
                </itemHandlingStructure>
                <itemLife>
                    <itemLifeDays>99</itemLifeDays>
                    <itemLifeType>
                        <itemLifeTypeCd></itemLifeTypeCd>
                    </itemLifeType>
                    <itemLifeTypeSymbol>
                        <itemLifeTypeSymbolCd></itemLifeTypeSymbolCd>
                    </itemLifeTypeSymbol>
                </itemLife>
                <itemLife>
                    <itemLifeDays>182</itemLifeDays>
                    <itemLifeType>
                        <itemLifeTypeCd></itemLifeTypeCd>
                    </itemLifeType>
                    <itemLifeTypeSymbol>
                        <itemLifeTypeSymbolCd></itemLifeTypeSymbolCd>
                    </itemLifeTypeSymbol>
                </itemLife>
                <itemLife>
                    <itemLifeDays>99</itemLifeDays>
                    <itemLifeType>
                        <itemLifeTypeCd></itemLifeTypeCd>
                    </itemLifeType>
                    <itemLifeTypeSymbol>
                        <itemLifeTypeSymbolCd></itemLifeTypeSymbolCd>
                    </itemLifeTypeSymbol>
                </itemLife>
                <itemStatusRecord>
                    <itemStatusCd>READY</itemStatusCd>
                </itemStatusRecord>
                <jurisdictionItemCharge>
                    <effectiveDt>2019-07-10+01:00</effectiveDt>
                    <chargePurposeType>
                        <chargePurposeTypeCd>SELLING</chargePurposeTypeCd>
                    </chargePurposeType>
                    <jurisdictionCharge>
                        <effectiveDt>2019-07-10+01:00</effectiveDt>
                        <chargeType>
                            <chargeTypeCd>ZERO</chargeTypeCd>
                            <chargeGroup>
                                <chargeGroupCd>VAT_GST</chargeGroupCd>
                                <chargeGroupDesc>VAT_GST</chargeGroupDesc>
                            </chargeGroup>
                        </chargeType>
                        <jurisdiction>
                            <jurisdictionCd>CHARGES</jurisdictionCd>
                        </jurisdiction>
                    </jurisdictionCharge>
                </jurisdictionItemCharge>                 
                <unitOfMeasure>
                    <uomCd>EACH</uomCd>
                </unitOfMeasure>
            </item>
        </itemObject>       
    </body>
</PublishItem>

<PublishItem xmlns:ebm="http://">

    <body>
        <itemObject>
            <item>
                <inventoryItemIndDD>1</inventoryItemIndDD>
                <variableWeightInd>0</variableWeightInd>
                <itemDescription>
                    <itemDesc>PEA &amp; BEAN MIX</itemDesc>
                    <itemName>WR ESS FRZ GB Pea and Bean Mix</itemName>
                </itemDescription>
                <itemEnvironmentRecord>
                    <environmentType>
                        <environmentTypeCd></environmentTypeCd>
                    </environmentType>
                </itemEnvironmentRecord>
                <itemGroupMembership>
                    <itemGroup>
                        <itemGroupLine>
                            <lineNum>24872</lineNum>
                        </itemGroupLine>
                    </itemGroup>
                </itemGroupMembership>
                <itemHandlingLevelType>
                    <itemHandlingLevelTypeCd>BASE</itemHandlingLevelTypeCd>
                </itemHandlingLevelType>
                <itemHandlingStructure>
                    <childItemQty>
                        <code>EACH</code>
                        <number>12.0</number>
                    </childItemQty>
                    <parentItem>
                        <inventoryItemIndDD>1</inventoryItemIndDD>
                        <variableWeightInd>0</variableWeightInd>
                        <itemDescription>
                            <itemDesc>PEA &amp; BEAN MIX</itemDesc>
                            <itemName>WR ESS FRZ GB Pea and Bean Mix</itemName>
                        </itemDescription>
                        <itemEnvironmentRecord>
                            <environmentType>
                                <environmentTypeCd>AMBIENT</environmentTypeCd>
                            </environmentType>
                        </itemEnvironmentRecord>
                        <itemGroupMembership>
                            <itemGroup>
                                <itemGroupLine>
                                    <lineNum>24872</lineNum>
                                </itemGroupLine>
                            </itemGroup>
                        </itemGroupMembership>
                        <itemHandlingLevelType>
                            <itemHandlingLevelTypeCd>BASE</itemHandlingLevelTypeCd>
                        </itemHandlingLevelType>
                        <itemLife>
                            <itemLifeDays>99</itemLifeDays>
                            <itemLifeType>
                                <itemLifeTypeCd></itemLifeTypeCd>
                            </itemLifeType>
                            <itemLifeTypeSymbol>
                                <itemLifeTypeSymbolCd></itemLifeTypeSymbolCd>
                            </itemLifeTypeSymbol>
                        </itemLife>
                        <itemLife>
                            <itemLifeDays>364</itemLifeDays>
                            <itemLifeType>
                                <itemLifeTypeCd></itemLifeTypeCd>
                            </itemLifeType>
                            <itemLifeTypeSymbol>
                                <itemLifeTypeSymbolCd></itemLifeTypeSymbolCd>
                            </itemLifeTypeSymbol>
                        </itemLife>
                        <itemLife>
                            <itemLifeDays>99</itemLifeDays>
                            <itemLifeType>
                                <itemLifeTypeCd></itemLifeTypeCd>
                            </itemLifeType>
                            <itemLifeTypeSymbol>
                                <itemLifeTypeSymbolCd></itemLifeTypeSymbolCd>
                            </itemLifeTypeSymbol>
                        </itemLife>
                        <itemStatusRecord>
                            <itemStatusCd></itemStatusCd>
                        </itemStatusRecord>
                        <stockItemPhysicalAttributes>
                            <casesPerLayerQty>6</casesPerLayerQty>
                            <depthQty>
                                <code>MM</code>
                                <number>380.0</number>
                            </depthQty>
                            <heightQty>
                                <code>MM</code>
                                <number>230.0</number>
                            </heightQty>
                            <layersPerPalletQty>10</layersPerPalletQty>
                            <widthQty>
                                <code>MM</code>
                                <number>290.0</number>
                            </widthQty>
                        </stockItemPhysicalAttributes>                       
                    </parentItem>
                </itemHandlingStructure>
                <itemLife>
                    <itemLifeDays>99</itemLifeDays>
                    <itemLifeType>
                        <itemLifeTypeCd></itemLifeTypeCd>
                    </itemLifeType>
                    <itemLifeTypeSymbol>
                        <itemLifeTypeSymbolCd></itemLifeTypeSymbolCd>
                    </itemLifeTypeSymbol>
                </itemLife>
                <itemLife>
                    <itemLifeDays>364</itemLifeDays>
                    <itemLifeType>
                        <itemLifeTypeCd></itemLifeTypeCd>
                    </itemLifeType>
                    <itemLifeTypeSymbol>
                        <itemLifeTypeSymbolCd></itemLifeTypeSymbolCd>
                    </itemLifeTypeSymbol>
                </itemLife>
                <itemLife>
                    <itemLifeDays>99</itemLifeDays>
                    <itemLifeType>
                        <itemLifeTypeCd></itemLifeTypeCd>
                    </itemLifeType>
                    <itemLifeTypeSymbol>
                        <itemLifeTypeSymbolCd></itemLifeTypeSymbolCd>
                    </itemLifeTypeSymbol>
                </itemLife>
                <itemStatusRecord>
                    <itemStatusCd>READY</itemStatusCd>
                </itemStatusRecord>
                <jurisdictionItemCharge>
                    <effectiveDt>2019-07-10+01:00</effectiveDt>
                    <chargePurposeType>
                        <chargePurposeTypeCd>SELLING</chargePurposeTypeCd>
                    </chargePurposeType>
                    <jurisdictionCharge>
                        <effectiveDt>2019-07-10+01:00</effectiveDt>
                        <chargeType>
                            <chargeTypeCd>ZERO</chargeTypeCd>
                            <chargeGroup>
                                <chargeGroupCd>VAT_GST</chargeGroupCd>
                                <chargeGroupDesc>VAT_GST</chargeGroupDesc>
                            </chargeGroup>
                        </chargeType>
                        <jurisdiction>
                            <jurisdictionCd>CHARGES</jurisdictionCd>
                        </jurisdiction>
                    </jurisdictionCharge>
                </jurisdictionItemCharge>
            </item>
        </itemObject>       
    </body>
</PublishItem>

Your help and suggestion on this will be great.

I think this is what you need. Please give this a try.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html> 
            <body>
                <h2>Conversion</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th style="text-align:left">inventoryItemIndDD</th>
                        <th style="text-align:left">variableWeightInd</th>
                        <th style="text-align:left">itemDesc</th>
                        <th style="text-align:left">itemName</th>
                        <th style="text-align:left">environmentTypeCd</th>
                        <th style="text-align:left">lineNum</th>
                        <th style="text-align:left">itemHandlingLevelType</th>
                        <th style="text-align:left">Caseitem:inventoryItemIndDD</th>
                        <th style="text-align:left">Caseitem:variableWeightInd</th>
                        <th style="text-align:left">Caseitem:itemDesc</th>
                        <th style="text-align:left">Caseitem:itemName</th>
                        <th style="text-align:left">Caseitem:environmentTypeCd</th>
                        <th style="text-align:left">Caseitem:lineNum</th>
                        <th style="text-align:left">Caseitem:itemHandlingLevelType</th>
                        <th style="text-align:left">Caseitem:itemLife1Days</th>
                        <th style="text-align:left">Caseitem:itemLife2Days</th>
                        <th style="text-align:left">Caseitem:itemLife3Days</th>
                    </tr>

                    <xsl:for-each select="//PublishItem">               
                        <xsl:for-each select="body/itemObject/item">
                            <tr>
                                <td>
                                    <xsl:value-of select="inventoryItemIndDD"/>
                                </td>
                                <td>
                                    <xsl:value-of select="variableWeightInd"/>
                                </td>
                                <td>
                                    <xsl:value-of select="itemDescription/itemDesc"/>
                                </td>
                                <td>
                                    <xsl:value-of select="itemDescription/itemName"/>
                                </td>
                                <td>
                                    <xsl:value-of select="itemEnvironmentRecord/environmentType/environmentTypeCd"/>
                                </td>
                                <td>
                                    <xsl:value-of select="itemGroupMembership/itemGroup/itemGroupLine/lineNum"/>
                                </td>
                                <td>
                                    <xsl:value-of select="itemHandlingLevelType/itemHandlingLevelTypeCd"/>
                                </td>

                                <td>
                                    <xsl:value-of select="itemHandlingStructure/parentItem/inventoryItemIndDD"/>
                                </td>
                                <td>
                                    <xsl:value-of select="itemHandlingStructure/parentItem/variableWeightInd"/>
                                </td>
                                <td>
                                    <xsl:value-of select="itemHandlingStructure/parentItem/itemDescription/itemDesc"/>
                                </td>
                                <td>
                                    <xsl:value-of select="itemHandlingStructure/parentItem/itemDescription/itemName"/>
                                </td>
                                <td>
                                    <xsl:value-of select="itemHandlingStructure/parentItem/itemEnvironmentRecord/environmentType/environmentTypeCd"/>
                                </td>
                                <td>
                                    <xsl:value-of select="itemHandlingStructure/parentItem/itemGroupMembership/itemGroup/itemGroupLine/lineNum"/>
                                </td>
                                <td>
                                    <xsl:value-of select="itemHandlingStructure/parentItem/itemHandlingLevelType/itemHandlingLevelTypeCd"/>
                                </td>
                                <td>
                                    <xsl:value-of select="itemHandlingStructure/parentItem/itemLife[1]/itemLifeDays"/>
                                </td> 
                                <td>
                                    <xsl:value-of select="itemHandlingStructure/parentItem/itemLife[2]/itemLifeDays"/>
                                </td>
                                <td>
                                    <xsl:value-of select="itemHandlingStructure/parentItem/itemLife[3]/itemLifeDays"/>
                                </td>                           
                            </tr>
                        </xsl:for-each>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM