Skip to content

Instantly share code, notes, and snippets.

@emchateau
Created July 1, 2020 04:48
Show Gist options
  • Save emchateau/23e45d4c07b89a725866123ef0156516 to your computer and use it in GitHub Desktop.
Save emchateau/23e45d4c07b89a725866123ef0156516 to your computer and use it in GitHub Desktop.
For each group
<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>
<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>
<!-- https://stackoverflow.com/questions/19115109/how-to-use-for-each-group-in-xsl/19116371#19116371 -->
<?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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment