[英]group by xml document by child element
我有以下 xml。 它是一组包含标识符、数量和类别的记录父节点。
<Data>
<record>
<identifier>1000</identifier>
<category>B</category>
<quantity>90.00</quantity>
</record>
<record>
<identifier>1000</identifier>
<category>B</category>
<quantity>50.00</quantity>
</record>
<record>
<identifier>1001</identifier>
<category>B</category>
<quantity>13.00</quantity>
</record>
<record>
<identifier>1002</identifier>
<category>B</category>
<quantity>100.00</quantity>
</record>
我需要按子“标识符”的记录元素进行分组。 预期的输出是这样的。
<Data>
<records>
<record>
<identifier>1000</identifier>
<category>B</category>
<quantity>90.00</quantity>
</record>
<record>
<identifier>1000</identifier>
<category>B</category>
<quantity>50.00</quantity>
</record>
</records>
<records>
<record>
<identifier>1001</identifier>
<category>B</category>
<quantity>13.00</quantity>
</record>
</records>
<records>
<record>
<identifier>1002</identifier>
<category>B</category>
<quantity>100.00</quantity>
</record>
</records>
我正在使用这个 xslt,但它在分组级别也不起作用。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:template match="Data">
<Data>
<xsl:for-each-group select="record" group-by="identifier">
<records>
<xsl:for-each select="current-group()">
<xsl:copy>
<xsl:value-of select="*" />
</xsl:copy>
</xsl:for-each>
</records>
</xsl:for-each-group>
</Data>
</xsl:template>
我该如何修复它以使其正常工作?
您显示的结果可以通过稍微调整(和简化)您的样式表来实现:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="/Data">
<Data>
<xsl:for-each-group select="record" group-by="identifier">
<records>
<xsl:copy-of select="current-group()" />
</records>
</xsl:for-each-group>
</Data>
</xsl:template>
</xsl:stylesheet>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.