简体   繁体   中英

Use XSLT to do a group by on one element in an XML

I am trying to do a group by based on a single element "h_order_num" in XML.

ORGINAL XML LOOKS LIKE THIS

<?xml version="1.0" encoding="UTF-8"?>
<saphana>
   <row>
      <h_brand_code>DDH</h_brand_code>
      <h_brand_country>USA</h_brand_country>
      <h_order_num>0400000631</h_order_num>
   </row>
   <row>
      <h_brand_code>DDF</h_brand_code>
      <h_brand_country>France</h_brand_country>
      <h_order_num>0400000631</h_order_num>
   </row>
   <row>
      <h_brand_code>DDG</h_brand_code>
      <h_brand_country>Germany</h_brand_country>
      <h_order_num>0400000634</h_order_num>
   </row>
</saphana>

THIS IS WHAT I AM TRYING TO ACHIEVE

<?xml version="1.0" encoding="UTF-8"?>
<saphana>
   <row>
      <Ordernumber>
         <Value>0400000631</Value>
         <LineItems>
            <h_brand_code>DDH</h_brand_code>
            <h_brand_country>USA</h_brand_country>
         </LineItems>
         <LineItems>
            <h_brand_code>DDF</h_brand_code>
            <h_brand_country>France</h_brand_country>
         </LineItems>
      </Ordernumber>
   </row>
   <row>
      <Ordernumber>
         <Value>0400000634</Value>
         <LineItems>
            <h_brand_code>DDG</h_brand_code>
            <h_brand_country>Gernamy</h_brand_country>
         </LineItems>
      </Ordernumber>
   </row>
</saphana>

Please help me writing an xslt for this tansformation

**Please Check 2.0**

        <xsl:template match="saphana">
        <xsl:copy>
            <xsl:for-each-group select="row" group-by="h_order_num">
                <xsl:copy>
                    <Ordernumber>
                        <Value>
                            <xsl:value-of select="current-grouping-key()"/>
                        </Value>
                        <xsl:for-each select=" current-group()">
                            <LineItems>
                                <h_brand_code><xsl:value-of select="child::h_brand_code"/></h_brand_code>
                                <h_brand_country><xsl:value-of select="child::h_brand_country"/></h_brand_country>
                            </LineItems>
                        </xsl:for-each>
                    </Ordernumber>
                </xsl:copy>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>

Please check This code:

<xsl:key name="loc1" match="row" use="h_order_num"/>
<xsl:template match="saphana">
    <xsl:copy>
        <xsl:for-each select="row[count(. | key('loc1', h_order_num)[1]) = 1]">
            <xsl:copy>
                <Ordernumber>
                    <value>
                        <xsl:value-of select="h_order_num"/>
                    </value>
                    <xsl:for-each select="key('loc1', h_order_num)">
                        <LineItems>
                            <h_brand_code>
                                <xsl:value-of select="h_brand_code"/>
                            </h_brand_code>
                            <h_brand_country>
                                <xsl:value-of select="h_brand_country"/>
                            </h_brand_country>
                        </LineItems>
                    </xsl:for-each>
                </Ordernumber>
            </xsl:copy>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

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