我需要将此节点合并在一起,但它必须与具有属性的父节点合并method="a"
输入:
<myroot>
<elem name="a" creationDate="">
<list id="xxx" ver="uu">
<nodeA id="a">
<fruit id="small">
<orange id="x" method="create">
<color>Orange</color>
</orange>
</fruit>
<fruit id="small" method="a">
<kiwi id="y" method="create">
<color>Red</color>
<type>sour</type>
</kiwi>
</fruit>
<fruit id="large" method="a">
<melon id="y" method="create">
<attributes>
<color>Green</color>
<type>sweet</type>
</attributes>
</melon>
</fruit>
</nodeA>
</list>
</elem>
</myroot>
我的 XSL 文件:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* , node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="list">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:for-each-group select="/*/*/*/*" group-by="@id">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:for-each-group select="current-group()/*" group-by="concat(local-name(), '|', @id)">
<xsl:copy>
<xsl:apply-templates select="@*, *, (current-group() except .)/*"/>
</xsl:copy>
</xsl:for-each-group>
</xsl:copy>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我的输出:
<myroot>
<elem name="a" creationDate="">
<list id="xxx" ver="uu">
<nodeA id="a">
<fruit id="small">
<orange id="x" method="create">
<color>Orange</color>
</orange>
<kiwi id="y" method="create">
<color>Red</color>
<type>sour</type>
</kiwi>
</fruit>
<fruit id="large" method="a">
<melon id="y" method="create">
<attributes>
<color>Green</color>
<type>sweet</type>
</attributes>
</melon>
</fruit>
</nodeA>
</list>
</elem>
</myroot>
预期输出:
<myroot>
<elem name="a" creationDate="">
<list id="xxx" ver="uu">
<nodeA id="a">
<fruit id="small" method="a"> <!-- this is the correct merge where the merged is in parent that has a method -->
<kiwi id="y" method="create">
<color>Red</color>
<type>sour</type>
</kiwi>
<orange id="x" method="create">
<color>Orange</color>
</orange>
</fruit>
<fruit id="large" method="a">
<melon id="y" method="create">
<attributes>
<color>Green</color>
<type>sweet</type>
</attributes>
</melon>
</fruit>
</nodeA>
</list>
</elem>
</myroot>
正如我们所看到的,我的转换只是将它结合在一起,并没有考虑方法。如何更改它以便将其合并到method="a"
示例中的父级<fruit id="small" method="a">
谢谢你。约翰