这是基于我对问题 #825783的回答的解决方案:
样式表
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kNode" match="NodeE" use="@Name"/>
<!--
Identity transform: copy elements and attributes from input file as is
-->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<!--
Use Muenchian grouping to apply unique NodeE elements.
See http://www.jenitennison.com/xslt/grouping/muenchian.html
-->
<xsl:template match="NodeE[generate-id() =
generate-id(key('kNode', @Name)[1])]">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<!--
Apply <NodeE> elements with the same @Name attribute value as the current
element with the "concat" mode enabled
-->
<xsl:apply-templates select="key('kNode', @Name)" mode="concat"/>
</xsl:copy>
</xsl:template>
<xsl:template match="NodeE" mode="concat">
<xsl:value-of select="."/>
<!-- Add comma except if this is the last node -->
<xsl:if test="position() != last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:template>
<!-- Drop other <NodeE> elements -->
<xsl:template match="NodeE"/>
</xsl:stylesheet>
输入
使用 JLRishe 提供的输入:
<Nodes>
<NodeA NodeAattr="123">
<NodeB NodeBattr="456"></NodeB>
<NodeC>
<NodeD NodeDAttr="ValueD">
<NodeE Name="ValueABC"> "555" </NodeE>
<NodeE Name="ValueABC"> "666" </NodeE>
<NodeE Name="ValueDEF"> "555" </NodeE>
<NodeE Name="ValueDEF"> "565" </NodeE>
<NodeE Name="ValueDEF"> "575" </NodeE>
<NodeE Name="ValueABC"> "595" </NodeE>
</NodeD>
</NodeC>
</NodeA>
</Nodes>
输出
这与 JLRishe 的输出不同,因为我对需求的理解不同:
<?xml version="1.0" encoding="utf-8"?>
<Nodes>
<NodeA NodeAattr="123">
<NodeB NodeBattr="456"/>
<NodeC>
<NodeD NodeDAttr="ValueD">
<NodeE Name="ValueABC"> "555" , "666" , "595" </NodeE>
<NodeE Name="ValueDEF"> "555" , "565" , "575" </NodeE>
</NodeD>
</NodeC>
</NodeA>
</Nodes>