how to apply group by on xslt elements
I need to group the value based on some attribute and populate it.
below mentioned is i/p xml and if you see there are 4 rows for Users and for id 2,4 Division is same i.e. HR
while generating actual o/p I need to group by Division ... Any help ???
I/P XML
<Users>
<User id="2" name="ABC" Division="HR"/>
<User id="3" name="xyz" Division="Admin"/>
<User id="4" name="LMN" Division="Payroll"/>
<User id="5" name="PQR" Division="HR"/>
</Users>
expected Result: I need to group the values based on Division and populate i.e.
<AllUsers>
<Division value="HR">
<User>
<id>2</id>
<name>ABC</name>
</User>
<User>
<id>5</id>
<name>PQR</name>
</User>
</Division>
<Division value="ADMIN">
<User>
<id>3</id>
<name>XYZ</name>
</User>
</Division>
<Division value="Payroll">
<User>
<id>4</id>
<name>LMN&l开发者_StackOverflow社区t;/name>
</User>
</Division>
</AllUsers>
In XSLT 1.0, using Muenchian grouping.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:key name="division" match="User" use="@Division" />
<xsl:template match="Users">
<AllUsers>
<xsl:apply-templates select="User[generate-id(.)=generate-id(key('division',@Division)[1])]"/>
</AllUsers>
</xsl:template>
<xsl:template match="User">
<Division value="{@Division}">
<xsl:for-each select="key('division', @Division)">
<User>
<id><xsl:value-of select="@id" /></id>
<name><xsl:value-of select="@name" /></name>
</User>
</xsl:for-each>
</Division>
</xsl:template>
</xsl:stylesheet>
In XSLT 2.0, use xsl:foreach-group
<xsl:output method="xml" indent="yes" />
<xsl:template match="Users">
<AllUsers>
<xsl:for-each-group select="User" group-by="@Division">
<Division value="{@Division}">
<xsl:for-each select="current-group()">
<User>
<id><xsl:value-of select="@id" /></id>
<name><xsl:value-of select="@name" /></name>
</User>
</xsl:for-each>
</Division>
</xsl:for-each-group>
</AllUsers>
</xsl:template>
use
<xsl:for-each-group select="*" group-by="@Division">
....
</xsl:for-each-group>
I don't prefer adding elements like that and this is what i will do. This works perfectly. It gives the output you need. Try it out.
XML
<?xml version="1.0"?>
<Users>
<User id="2" name="ABC" Division="HR"/>
<User id="3" name="xyz" Division="Admin"/>
<User id="4" name="LMN" Division="Payroll"/>
<User id="5" name="PQR" Division="HR"/>
</Users>
XSLT 1.0
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:key name="division" match="User" use="@Division" />
<xsl:template match="Users">
<xsl:element name="AllUsers">
<xsl:apply-templates select="User[generate-id(.)=generate-id(key('division',@Division)[1])]" />
</xsl:element>
</xsl:template>
<xsl:template match="User">
<xsl:element name="Division">
<xsl:attribute name="value">
<xsl:value-of select="@Division" />
</xsl:attribute>
<xsl:for-each select="key('division', @Division)">
<xsl:element name="User">
<xsl:element name="id">
<xsl:value-of select="@id" />
</xsl:element>
<xsl:element name="name">
<xsl:value-of select="@name" />
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
XSLT 2.0
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:key name="division" match="User" use="@Division" />
<xsl:template match="Users">
<xsl:element name="AllUsers">
<xsl:for-each-group select="*" group-by="@Division">
<xsl:element name="Division">
<xsl:attribute name="value">
<xsl:value-of select="@Division" />
</xsl:attribute>
<xsl:for-each select="current-group()">
<xsl:element name="User">
<xsl:element name="id">
<xsl:value-of select="@id" />
</xsl:element>
<xsl:element name="name">
<xsl:value-of select="@name" />
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each-group>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Using either will give you this output,
<AllUsers>
<Division value="HR">
<User>
<id>2</id>
<name>ABC</name>
</User>
<User>
<id>5</id>
<name>PQR</name>
</User>
</Division>
<Division value="Admin">
<User>
<id>3</id>
<name>xyz</name>
</User>
</Division>
<Division value="Payroll">
<User>
<id>4</id>
<name>LMN</name>
</User>
</Division>
</AllUsers>
精彩评论