按 xml 文档按 child 元素分组
group by xml document by child element
我有以下xml。它是一组记录 parent 节点,包含标识符、数量和类别。
<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>
我需要根据 child "identifier" 的记录元素进行分组。
预期的输出是这样的。
<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>
我有以下xml。它是一组记录 parent 节点,包含标识符、数量和类别。
<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>
我需要根据 child "identifier" 的记录元素进行分组。 预期的输出是这样的。
<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>