countrymeister
asked on
Converting an xml to another xml using xsl and group by function
I have an xml that I need to parse to another xml, I tried to use the group by function, but it traverses just the first iteration
Here is the input XML
XML1
--------
<?xml version="1.0" encoding=utf-8"?>
<Resultsets>
<Record>
<App>SmokeTest </App>
<Location>Germany</Locatio n>
<Division>Parts</Division>
<Desk>Help</Desk>
<Product>Bulldozer</Produc t>
<Access>Variable</Access>
</Record>
<Record>
<App>SmokeTest </App>
<Location>Germany</Locatio n>
<Division>Supplies</Divisi on>
<Desk>Help</Desk>
<Product>Bulldozer</Produc t>
<Access>Fixed</Access>
</Record>
.
.
.
</Resultsets>
An this is the desired output
Location name "Germany">
<Division name ="Parts">
<Desk name ="Help">
<Product name="XYZ>
<Permission name="Variable"></Permissi on>
<Permission name="Test"></Permission>
</Product>
<Product name="XYZ>
<Permission name="Variable"></Permissi on>
<Permission name="Test"></Permission>
</Product>
</Desk>
<Desk name ="Supplies">
<Product name="XYZ>
<Permission name="Variable"></Permissi on>
<Permission name="Test"></Permission>
</Product>
</Desk>
</Division>
<Division name ="Parts">
<Desk name ="Help">
<Product name="XYZ>
<Permission name="Variable"></Permissi on>
<Permission name="Test"></Permission>
</Product>
</Desk>
<Desk name ="Supplies">
<Product name="XYZ>
<Permission name="Variable"></Permissi on>
<Permission name="Test"></Permission>
</Product>
</Desk>
</Division>
</Location>
And this is what I have so far
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:key name="loc-by-name" match="ResultSet1/*" use="LOCATION"/>
<xsl:template match="/">
<apps>
<app>
<xsl:apply-templates/>
</app>
</apps>
</xsl:template>
<xsl:template match="ResultSet1">
<xsl:for-each-group select="*[generate-id() = generate-id(key('loc-by-na me', LOCATION)[1])]" group-by="LOCATION">
<xsl:sort data-type="text" select="LOCATION"/>
<location name="{current-grouping-ke y()}">
<xsl:for-each-group select="current-group()" group-by="DIVISION">
<xsl:sort data-type="text" select="DIVISION"/>
<division name="{current-grouping-ke y()}">
<xsl:for-each-group select="current-group()" group-by="DESK">
<xsl:sort data-type="text" select="DESK"/>
<desk name="{current-grouping-ke y()}">
<xsl:for-each-group select="current-group()" group-by="PRODUCT">
<xsl:sort data-type="text" select="PRODUCT"/>
<product name="{current-grouping-ke y()}">
<xsl:for-each-group select="current-group()" group-by="PERMISSION">
<xsl:sort data-type="text" select="PERMISSION"/>
<permission name="{current-grouping-ke y()}">
</permission>
</xsl:for-each-group>
</product>
</xsl:for-each-group>
</desk>
</xsl:for-each-group>
</division>
</xsl:for-each-group>
</location>
<!-- <xsl:for-each select="key('child-by-name ', LOCATION)"> -->
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
Here is the input XML
XML1
--------
<?xml version="1.0" encoding=utf-8"?>
<Resultsets>
<Record>
<App>SmokeTest </App>
<Location>Germany</Locatio
<Division>Parts</Division>
<Desk>Help</Desk>
<Product>Bulldozer</Produc
<Access>Variable</Access>
</Record>
<Record>
<App>SmokeTest </App>
<Location>Germany</Locatio
<Division>Supplies</Divisi
<Desk>Help</Desk>
<Product>Bulldozer</Produc
<Access>Fixed</Access>
</Record>
.
.
.
</Resultsets>
An this is the desired output
Location name "Germany">
<Division name ="Parts">
<Desk name ="Help">
<Product name="XYZ>
<Permission name="Variable"></Permissi
<Permission name="Test"></Permission>
</Product>
<Product name="XYZ>
<Permission name="Variable"></Permissi
<Permission name="Test"></Permission>
</Product>
</Desk>
<Desk name ="Supplies">
<Product name="XYZ>
<Permission name="Variable"></Permissi
<Permission name="Test"></Permission>
</Product>
</Desk>
</Division>
<Division name ="Parts">
<Desk name ="Help">
<Product name="XYZ>
<Permission name="Variable"></Permissi
<Permission name="Test"></Permission>
</Product>
</Desk>
<Desk name ="Supplies">
<Product name="XYZ>
<Permission name="Variable"></Permissi
<Permission name="Test"></Permission>
</Product>
</Desk>
</Division>
</Location>
And this is what I have so far
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:key name="loc-by-name" match="ResultSet1/*" use="LOCATION"/>
<xsl:template match="/">
<apps>
<app>
<xsl:apply-templates/>
</app>
</apps>
</xsl:template>
<xsl:template match="ResultSet1">
<xsl:for-each-group select="*[generate-id() = generate-id(key('loc-by-na
<xsl:sort data-type="text" select="LOCATION"/>
<location name="{current-grouping-ke
<xsl:for-each-group select="current-group()" group-by="DIVISION">
<xsl:sort data-type="text" select="DIVISION"/>
<division name="{current-grouping-ke
<xsl:for-each-group select="current-group()" group-by="DESK">
<xsl:sort data-type="text" select="DESK"/>
<desk name="{current-grouping-ke
<xsl:for-each-group select="current-group()" group-by="PRODUCT">
<xsl:sort data-type="text" select="PRODUCT"/>
<product name="{current-grouping-ke
<xsl:for-each-group select="current-group()" group-by="PERMISSION">
<xsl:sort data-type="text" select="PERMISSION"/>
<permission name="{current-grouping-ke
</permission>
</xsl:for-each-group>
</product>
</xsl:for-each-group>
</desk>
</xsl:for-each-group>
</division>
</xsl:for-each-group>
</location>
<!-- <xsl:for-each select="key('child-by-name
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER