Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to apply group by on xslt elements

Tags:

xslt

grouping

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</name>   </User>   </Division> </AllUsers> 
like image 273
Amit Avatar asked Jan 27 '10 12:01

Amit


People also ask

How do I use each group in XSLT?

Just as the XSLT 1.0 xsl:for-each instruction iterates across a node set, with child elements of the xsl:for-each element specifying what you want done to each node in the set, the xsl:for-each-group instructions iterates across the groups, with children of the xsl:for-each-group element specifying what you want done ...

What is current group () in XSLT?

Returns the contents of the current group selected by xsl:for-each-group. Available in XSLT 2.0 and later versions. Available in all Saxon editions. current-group() ➔ item()*

How do you sum and group in XSLT?

Bookmark this question. Show activity on this post. For each "agency" node I need to find the "stmt" elements that have the same key1, key2, key3 values and output just one "stmt" node with the "comm" and "prem" values summed together.

How do you assign values in XSLT?

XSLT <xsl:variable> The <xsl:variable> element is used to declare a local or global variable. Note: The variable is global if it's declared as a top-level element, and local if it's declared within a template. Note: Once you have set a variable's value, you cannot change or modify that value!


2 Answers

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> 

like image 143
Lachlan Roche Avatar answered Sep 23 '22 16:09

Lachlan Roche


use

<xsl:for-each-group  select="*" group-by="@Division">  .... </xsl:for-each-group>  

check out this example: http://www.zvon.org/xxl/XSL-Ref/Tutorials/For-Each-Group/feg1.html

like image 31
Pentium10 Avatar answered Sep 19 '22 16:09

Pentium10