I am attempting to process the following XML to display each Discount record with each MerchandiseItem using XSLT:
NOTE: Each MerchandiseItem is always followed by 0 or more Discount elements
Sample XML:
<IBMGSA xmlns="http://www.omnicogroup.com/FPF/namespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<transaction>
<MerchandiseItem Record="2">
<Identifier>
<SalePrc>6.14</SalePrc>
<Quantity>1.00</Quantity>
<NetQuantity>1.00</NetQuantity>
<SaleQuantity>1</SaleQuantity>
<SalePrice>6.14</SalePrice>
<Penny>.01</Penny>
<Zero>0.00</Zero>
<IsVoided>0</IsVoided>
<VoidedQuantity>0.00</VoidedQuantity>
06</Identifier>
<ItemCode>
<ItemCodeGTIN14>00000000000083</ItemCodeGTIN14>
<ItemCodeGTIN12>000000000008</ItemCodeGTIN12>
<Sku>000000008</Sku>
<SkuType>3</SkuType>
<SkuByte>1</SkuByte>
<SuppressRecord>NO</SuppressRecord>
<ItemDescription>ZIL PLAY A-</ItemDescription>
<ItemDescTrimmed>ZIL PLAYA-SYS</ItemDescTrimmed>
1000000000000000000083</ItemCode>
<Department>
<DeptSuppress>YES</DeptSuppress>
182</Department>
<ItemAmount>
<NegativeTransaction>0</NegativeTransaction>
<ItemAmount>7.68</ItemAmount>
<ItemDiscountCode>2</ItemDiscountCode>
<ItemDiscountAmount>1.54</ItemDiscountAmount>
<ItemDiscountPercent>20.0</ItemDiscountPercent>
<ItemAmt>7.68</ItemAmt>
<ItemDisAmt>6.14</ItemDisAmt>
</ItemAmount> <!-- Added by edit -->
</MerchandiseItem>
<Discount Record="3">
<Identifier>10</Identifier>
<DiscountCode>2</DiscountCode>
<DiscountAmount>
<DiscountAmountType>PE</DiscountAmountType>
<DiscountQuantity>1</DiscountQuantity>
<NegativeTransaction>0</NegativeTransaction>
<DiscountAmount>1.54</DiscountAmount>
<TaxExemptAmount>0.00</TaxExemptAmount>
<ABSDiscountAmt>1.54</ABSDiscountAmt>
154</DiscountAmount>
<DiscountPercent>
<DiscountPercent>20.0</DiscountPercent>
200</DiscountPercent>
</Discount>
<Discount Record="4">
<Identifier>10</Identifier>
<DiscountCode>25</DiscountCode>
</Discount>
...
...
...
</transaction>
</IBMGSA> <!-- Added by edit -->
How could I traverse the DiscountItem elements while iterating through each MerchandiseItem element given that the DiscountItem elements always comes after the MerchandiseItem element and needs to be associated/displayed only with that MerchandiseItem?
I need the output to be as follows (referencing this sample XML) - thank you for reformatting! Thank you - the correct HTML output (version 5) should be:
<html>
<body>
<table>
<tr>
ZIL PLAY A-
00000000000083 1 @ 7.68 7.68
10 (20 %) OFF 00000000000083 1.54-
25 (5 %) OFF 00000000000083 .31-
</tr>
</table>
</body>
</html>
XML equivalent of output above:
<ItemDescription>ZIL PLAY A-</ItemDescription>
<ItemCodeGTIN14>00000000000083</ItemCodeGTIN14> <Quantity>1.00</Quantity> @ <ItemAmount>7.68</ItemAmount>
<Identifier>10</Identifier> (<DiscountPercent>20.0 %</DiscountPercent>) OFF <ItemCodeGTIN14>00000000000083</ItemCodeGTIN14> <DiscountAmount>1.54</DiscountAmount>-
<Identifier>25</Identifier> (<DiscountPercent>5 %</DiscountPercent>) OFF <ItemCodeGTIN14>00000000000083</ItemCodeGTIN14> <DiscountAmount>.31</DiscountAmount>-
Using the suggestion in comments from @michael.hor257k, I am trying to display the MerchandiseItem and Discount information using the following XSL (updated to be more clear - hopefully - of how I want to group and display the DiscountItem and MerchandiseItems listed above:
<?xml version="1.0"?>
<xsl:stylesheet xmlns="http://www.omnicogroup.com/FPF/namespace"
xmlns:ns1="http://www.omnicogroup.com/FPF/namespace"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<table>
<tr>
<xsl:for-each-group select="*" group-starting-
with="MerchandiseItem">
<b>Here is a MerchItem</b><br/> <!--Should repeat (16 times as there are 16 total MerchandiseItem elements in my XML payload. Currently -nothing is displayed and it does not appear that this for-each group is being entered at runtime)-->
</xsl:for-each-group>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
but nothing is rendering using the same XML example. Are my for-each loops not referencing the MerchandiseItem/Discount groupings correctly?
textoutput. You can create the output your want by referencing the desired element withxsl:value-of. But do it by yourself, because your question is unclear. - zx485MerchandiseItemis followed by 0 to many associatedDiscountelements, you could use<xsl:for-each-group select="*" group-starting-with="MerchandiseItem">to group each item with its discounts. Then process the discounts usingxsl:for-each select="current-group()/Discount". - michael.hor257k