Link to home
Start Free TrialLog in
Avatar of countrymeister
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</Location>
                <Division>Parts</Division>
                <Desk>Help</Desk>
                <Product>Bulldozer</Product>
                <Access>Variable</Access>
      </Record>
      <Record>  
                <App>SmokeTest </App>
               <Location>Germany</Location>
                <Division>Supplies</Division>
                <Desk>Help</Desk>
                <Product>Bulldozer</Product>
                <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"></Permission>
                    <Permission name="Test"></Permission>
            </Product>
           <Product name="XYZ>
                    <Permission name="Variable"></Permission>
                    <Permission name="Test"></Permission>
            </Product>
         </Desk>
         <Desk name ="Supplies">
             <Product name="XYZ>
                    <Permission name="Variable"></Permission>
                    <Permission name="Test"></Permission>
            </Product>          
         </Desk>
      </Division>
     <Division name ="Parts">
         <Desk name ="Help">
             <Product name="XYZ>
                    <Permission name="Variable"></Permission>
                    <Permission name="Test"></Permission>
            </Product>
            </Desk>
         <Desk name ="Supplies">
             <Product name="XYZ>
                    <Permission name="Variable"></Permission>
                    <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-name', LOCATION)[1])]" group-by="LOCATION">
            <xsl:sort data-type="text" select="LOCATION"/>
            <location name="{current-grouping-key()}">
            <xsl:for-each-group select="current-group()"  group-by="DIVISION">
                                          <xsl:sort data-type="text" select="DIVISION"/>                                          
                                          <division name="{current-grouping-key()}">
                                                            <xsl:for-each-group select="current-group()"  group-by="DESK">
                                                            <xsl:sort data-type="text" select="DESK"/>
                                                            <desk name="{current-grouping-key()}">
                                                                        <xsl:for-each-group select="current-group()"  group-by="PRODUCT">
                                                            <xsl:sort data-type="text" select="PRODUCT"/>
                                                            <product name="{current-grouping-key()}">
                                                            <xsl:for-each-group select="current-group()"  group-by="PERMISSION">
                                                            <xsl:sort data-type="text" select="PERMISSION"/>
                                                            <permission name="{current-grouping-key()}">
                                                            </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>

ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of countrymeister
countrymeister

ASKER

The group by function zooms thru, much faster than the previous solution i have a .NET pocess that i ran using xslt transform the first solution with keys took abou 8 secs processing, of course I have other functions in my process, but the group by zoomed thru in 2 secs