How to use for each group in XSL

im still learning for-each-group what is the best way of grouping something like this using XSL?(by country) i'm trying to use XSL to convert this XML to another XML.

<?xml version="1.0" encoding="UTF-8"?>
<Person> <Student> <Info Country="England" Name="Dan" Age="20" /> </Student> <Student> <Info Country="England" Name="Dan" Age="20" /> </Student> <Student> <Info Country="England" Name="Sam" Age="20" /> </Student> <Student> <Info Country="Australia" Name="David" Age="22" /> </Student> <Student> <Info Country="Australia" Name="David" Age="22" /> </Student>
</Person>
1

1 Answer

If you group by country you would start with e.g.

<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>

Then you have to decide whether you want to further group the Info elements in each country group, for instance by name:

<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>
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like