我有一个拥有如此多元素的xml,其中大部分包含属性..对于某些属性值是相同的,所以我需要对它们进行分组并生成diff xml.I/p Ex:
<TestNode>
<ABC1 value="10.7" format="$" />
<ABC2 value="10.5" format="$" />
<ABC3 value="20" format="Rs" />
<ABC4 value="50" format="Rs" />
<ABC5 value="10.5" format="$" />
</TestNode>
Run Code Online (Sandbox Code Playgroud)
我需要按格式对行进行分组. 注意:格式不固定......它可能会增长...... O/P Ex: 有可能获得吗?提前致谢...
在XSLT 1.0中,您将使用Muenchian分组.
定义一个关键的"格式",我们可以从中轻松选择给定格式名称的所有元素.比应用Muenchian分组来查找输入中的唯一格式.
然后它变得简单."*"模板将按格式应用一次,并使用key()获取该格式的所有条目.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:key name="format" match="TestNode/*" use="@format" />
<xsl:template match="TestNode">
<body>
<xsl:apply-templates select="*[generate-id(.)=generate-id(key('format',@format)[1])]"/>
</body>
</xsl:template>
<xsl:template match="*">
<format format="{@format}">
<xsl:copy-of select="key('format', @format)" />
</format>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
在 XSLT 2.0 中,您应该能够使用<xsl:for-each-group>
, current-grouping-key()
和current-group()
例子:
<xsl:for-each-group
select="TestNode/*"
group-by="@format"
>
<group format="{current-grouping-key()}">
<xsl:for-each select="current-group()">
<xsl:copy-of select="."/>
</xsl:for-each>
</group>
</xsl:for-each-group>
Run Code Online (Sandbox Code Playgroud)
请参阅: http: //www.w3.org/TR/xslt20/#grouping