XSLT对属性进行分组

时间:2011-05-19 04:22:27

标签: xslt grouping

输入文件格式为

<Node1 Attribute11="colors" >
    <Node2 Attribute21="Order1" Attribute22="123" Attribute23="red" />
    <Node2 Attribute21="Order2" Attribute22="456" Attribute23="blue" />
    <Node2 Attribute21="Order1" Attribute22="789" Attribute23="green" />
    <Node2 Attribute21="Order2" Attribute22="012" Attribute23="yellow" />
    <Node2 Attribute21="Order3" Attribute22="345" Attribute23="red" />
    <Node2 Attribute21="Order1" Attribute22="678" Attribute23="blue" />
</Node1>

所需的输出是

<Node1 Attribute11="colors" >
    <Node3 Attribute21="Order1" >
    <Node2 Attribute21="Order1" Attribute22="123" Attribute23="red" />
    <Node2 Attribute21="Order1" Attribute22="789" Attribute23="green" />
    <Node2 Attribute21="Order1" Attribute22="678" Attribute23="blue" />
    </Node3>
    <Node3 Attribute21="Order2">
    <Node2 Attribute21="Order2" Attribute22="456" Attribute23="blue" />
    <Node2 Attribute21="Order2" Attribute22="012" Attribute23="yellow" />
    </Node3>
    <Node3 Attribute21="Order3">
    <Node2 Attribute21="Order3" Attribute22="345" Attribute23="red" />
    </Node3>
</Node1>

通过分组,我无法以所需的格式获得输出。 任何有关XSLT 1.0的帮助都非常感谢

1 个答案:

答案 0 :(得分:1)

Saxon-HE 9.2.1.1

测试

XSLT 1.0 解决方案


循环解决方案:

 <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="byAttribute21" match="Node1/Node2" use="@Attribute21"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="Node1">
<Node1>
    <xsl:copy-of select="@*"/>
    <xsl:for-each select="Node2[generate-id()=generate-id(key('byAttribute21', @Attribute21)[1])]">
        <Node3 Attribute21="{@Attribute21}">
            <xsl:apply-templates select="key('byAttribute21', @Attribute21)"/>
        </Node3>
    </xsl:for-each>
 </Node1>
</xsl:template>

</xsl:stylesheet>

没有循环的解决方案:

<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="byAttribute21" match="Node1/Node2" use="@Attribute21"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
 </xsl:copy>
</xsl:template>

<xsl:template match="Node1">
    <Node1>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select="*" mode="node3"/>
    </Node1>
</xsl:template>

 <xsl:template match="Node2[generate-id()=generate-id(key('byAttribute21', @Attribute21)[1])]" mode="node3">
 <Node3 Attribute21="{@Attribute21}">
   <xsl:apply-templates select="key('byAttribute21',@Attribute21)" mode="node2"/>
  </Node3>
     </xsl:template>

     <xsl:template match="Node2" mode="node2">
         <xsl:copy-of select="."/>
     </xsl:template>

</xsl:stylesheet>