我希望输出 xml 根据属性“f”为元素“c”分组。这是我的输入 xml 和 xslt。我希望该组只出现一次,并且其他节点应该按原样复制到输出中。我尝试的 xslt 复制了整个输入 xml。因此,如果有两个或多个具有 c 元素且 'f' 的属性值相同的元素,则希望该组第一次出现在输出中。我想要的结果也被复制了。
输入xml
<M>
<a>
<b>
<c f="123">
<d>Al</d>
<e NO="678">
<f>Y</f>
<g>
<h>FTO</h>
</g>
</e>
</c>
</b>
</a>
<a>
<b>
<c f="123">
<d>Al</d>
<e NO="678">
<f>Y</f>
<g>
<h>FTO</h>
</g>
</e>
</c>
</b>
</a>
<a>
<b>
<c f="567">
<d>Al</d>
<e NO="678">
<f>Y</f>
<g>
<h>FTO</h>
</g>
</e>
</c>
</b>
</a>
<a>
<b>
<somethingelse></somethingelse>
</b>
</a>
</M>
想要输出xml
<M>
<a>
<b>
<c f="123">
<d>Al</d>
<e NO="678">
<f>Y</f>
<g>
<h>FTO</h>
</g>
</e>
</c>
</b>
</a>
<a>
<b>
<c f="567">
<d>Al</d>
<e NO="678">
<f>Y</f>
<g>
<h>FTO</h>
</g>
</e>
</c>
</b>
</a>
<a>
<b>
<somethingelse></somethingelse>
</b>
</a>
</M>
xslt 我试过了
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="mykey" match="c"
use="@f"/>
<xsl:template match=
"c[generate-id()
=
generate-id(key('mykey',@f)[1])
]
">
<xsl:text/>
<xsl:copy-of select="key('mykey',@f)[1]"/>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>