0
votes

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?

1
What would look like your desired XML output? And what should be your HTML output version (4.0, 4.01, 5.0)? And did I add the correct closing tags to your source XML? - zx485
You say you want HTML5 output, but provide the result of text output. You can create the output your want by referencing the desired element with xsl:value-of. But do it by yourself, because your question is unclear. - zx485
Thank you for reformatting the XML, I have updated the description to include the expected output I am trying to achieve...hopefully this makes sense what I am trying to do. - user3093222
I have provided the expected output (HTML) and XML equivalent. My question is, using the XSL for-each loop code I provided, how can I process this XML sample which has multiple MerchandiseItem elements along with associated (not structured as such) <Discount> elements in the expected format I included (HTML and XML). Thank you! - user3093222
Assuming that each MerchandiseItem is followed by 0 to many associated Discount elements, you could use <xsl:for-each-group select="*" group-starting-with="MerchandiseItem"> to group each item with its discounts. Then process the discounts using xsl:for-each select="current-group()/Discount". - michael.hor257k

1 Answers

0
votes

Here is a sample XSLT 3 (I think I have used no XSLT 3 features so should work with an XSLT 2 processor as well) stylesheet that shows how to apply for-each-group group-starting-with in some context to group and output the data:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        exclude-result-prefixes="#all"
        xpath-default-namespace="http://www.omnicogroup.com/FPF/namespace"
        version="3.0">


    <xsl:output method="html" indent="yes" html-version="5"/>

    <xsl:template match="/">
        <html>
            <head>
                <title>.NET XSLT Fiddle Example</title>
            </head>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="transaction">
        <table>
            <thead>
                <tr>
                    <th/>
                </tr>
            </thead>
            <xsl:for-each-group select="*" group-starting-with="MerchandiseItem">
                <tbody>
                    <xsl:apply-templates select="current-group()[self::Discount]">
                        <xsl:with-param name="merch-item" select="."/>
                    </xsl:apply-templates>
                </tbody>
            </xsl:for-each-group>
        </table>
    </xsl:template>

    <xsl:template match="Discount">
        <xsl:param name="merch-item"/>
        <tr>
            <td valign="top" align="left" style="font-size:14px;line-height:18px;">
                <xsl:value-of select="$merch-item/ItemCode/ItemDescription"/>
                <br />
                <xsl:value-of select="$merch-item/ItemCode/ItemCodeGTIN14"/>&#160;

                <xsl:value-of select="floor($merch-item/Identifier/Quantity)"/> @ <xsl:value-of 
    select="$merch-item/ItemAmount/ItemAmount div $merch-item/Identifier/Quantity"/>

                <xsl:value-of select="Identifier, DiscountPercent/DiscountPercent"/>
            </td>
        </tr>
    </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/ej9EGd3

I realize the output format is not the one you have shown but I couldn't really figure from the two different snippets which format exactly you want, the sample shows how to use the grouping construct and also process each Discount element in each group to output some value while allowing access to the MerchandiseItem starting each group so hopefully it suffices to allow you to mold that into the proper format yourself.