[英]Create element names dynamically and group data while parsing one xml to another xml using xslt
[英]Use XSLT to do a group by on one element in an XML
我正在尝试基于XML中的单个元素“ h_order_num”进行分组。
像这样的原始XML外观
<?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>
这是我要尝试的
<?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>
请帮我为此变形写一个xslt
**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>
请检查此代码:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.