我还在学习for-each-group
使用XSL分组这样的东西的最佳方法是什么?(按国家/地区)我正在尝试使用XSL将此XML转换为另一种XML。
<?xml version="1.0" encoding="UTF-8"?>
<Person>
<Student>
<Info Country="England" Name="Dan" Age="20" Class="C" />
</Student>
<Student>
<Info Country="England" Name="Dan" Age="20" Class="B" />
</Student>
<Student>
<Info Country="England" Name="Sam" Age="20" Class="A" />
</Student>
<Student>
<Info Country="Australia" Name="David" Age="22" Class="D" />
</Student>
<Student>
<Info Country="Australia" Name="David" Age="22" Class="A" />
</Student>
</Person>
答案 0 :(得分:28)
如果按国家/地区分组,则可以从例如
开始<xsl:template match="Person">
<xsl:for-each-group select="Student/Info" group-by="@Country">
<country name="{current-grouping-key()}">
</country>
</xsl:for-each-group>
</xsl:template>
然后,您必须决定是否要进一步对每个国家/地区组中的Info
元素进行分组,例如按名称:
<xsl:template match="Person">
<xsl:for-each-group select="Student/Info" group-by="@Country">
<country name="{current-grouping-key()}">
<xsl:for-each-group select="current-group()" group-by="@Name">
<student name="{current-grouping-key()}">
<classes>
<xsl:for-each select="current-group()">
<class><xsl:value-of select="@Class"/></class>
</xsl:for-each>
</classes>
</student>
</xsl:for-each-group>
</country>
</xsl:for-each-group>
</xsl:template>