Grouping XML elelments using XSLT – xsl:for-each-group

This is an informal post on XSLT for-each-group which is very powerful function to group xml elements based on some criteria.

For Ex, you may want to convert a flat structure, typically returned by DB into an xml which is, say department wise. for-each-group is what you want

This sample is to convert a flat structure into a projectOwner wise grouping

Input

                             
                                 
        101      
        T1      
        Act1Owner      
        1001          
        Prj1      
        PrjOwner      
        some desc 1
        comments1
                                 
                                 
        102      
        T2      
        Act2Owner      
        1002          
        Prj2      
        PrjOwner      
        some desc 2
        comments2
                                 

Ouput Format required


PrjOwner


101
T1
1001
Prj1
comments1


102
T2
1002
Prj2
comments2




PrjOwner2


103
T3
1002
Prj3
comments2


XSLT
——-

 
   

   

 


 
 

 
 



   
 

for-each-group groups the entire xml as per the group-by value defined and loads in memory
ex :  — ProjectOwner here
current-grouping-key() gives the values of all ProjectOwner
current-group() lists all the tags defined in a particular set
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s