[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] Re: Re: SOLVED - [xsl] flat structure to deep structure in a smart way


Subject: RE: [xsl] Re: Re: SOLVED - [xsl] flat structure to deep structure in a smart way
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Sat, 25 Sep 2004 09:49:01 +0100

This solution, with 5 levels of grouping, can actually be generalized to an
arbitrary depth by using recursion. It looks something like this:

<xsl:template name="group">
  <xsl:param name="population" as="element(line)*" required="yes"/>
  <xsl:param name="depth" as="xs:integer" select="1"/>
  <xsl:element name="substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ', $depth, 1)">
    <xsl:attribute name="Value"><xsl:value-of
                      select="current-grouping-key()"/>
    </xsl:attribute>
    <xsl:for-each-group select="$population"
group-by="item[@itemcount=$depth]">
      <xsl:call-template name="group">
        <xsl:with-param name="population" select="current-group()"/>
        <xsl:with-param name="depth" select="$depth+1"/>
      </
    </
  </
</

Michael Kay
http://www.saxonica.com/ 

> -----Original Message-----
> From: jozef.aerts@xxxxxxxxxxxxxx [mailto:jozef.aerts@xxxxxxxxxxxxxx] 
> Sent: 25 September 2004 09:34
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Re: Re: SOLVED - [xsl] flat structure to deep 
> structure in a smart way
> 
> 
> Dear Jeni, dear all others helping me with this problem.
> 
> Yesterday night, already in bed, I suddenly remembered having seen an
> article a few days ago about the new features of XSLT 2, describing
> something about grouping.
> Also this morning, I found Jennies mail with her suggestion 
> for XSLT 2.
> So I took my good young Saxon horse from stable, and started working
> with that. Thanks to Jeni's mail I could then solve the 
> problem in less
> than 15 minutes.
> Please find the stylesheet below.
> 
> I would dare to say that XSLT 2 is a blessing for mankind, as it makes
> many thinks considerably easier.
> I will definitely use XSLT 2 for my real project, where I need to
> convert a flat structure with 92 items into a deep structure with
> something like 8 levels of deepness. Guess doing that with XSLT1 is
> something like a nightmare.
> 
> Many thanks - this is a great mail list.
> 
> The code:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>   <xsl:output method="xml" version="1.0" encoding="UTF-8" 
> indent="yes"/>
>   <xsl:template match="root">
>     <xsl:element name="result">
>       <xsl:for-each-group select="line" 
> group-by="item[@itemcount = 1]">
>         <xsl:element name="A">
>           <xsl:attribute name="Value"><xsl:value-of
> select="current-grouping-key()"/></xsl:attribute>
>           <xsl:for-each-group select="current-group()"
> group-by="item[@itemcount = 2]">
>             <xsl:element name="B">
>               <xsl:attribute name="Value"><xsl:value-of
> select="current-grouping-key()"/></xsl:attribute>
>               <xsl:for-each-group select="current-group()"
> group-by="item[@itemcount = 3]">
>                 <xsl:element name="C">
>                   <xsl:attribute name="Value"><xsl:value-of
> select="current-grouping-key()"/></xsl:attribute>
>                   <xsl:for-each-group select="current-group()"
> group-by="item[@itemcount = 4]">
>                     <xsl:element name="D">
>                       <xsl:attribute name="Value"><xsl:value-of
> select="current-grouping-key()"/></xsl:attribute>
>                       <xsl:for-each select="item[@itemcount=5]">
>                         <xsl:element name="E">
>                           <xsl:attribute name="Value"><xsl:value-of
> select="."/></xsl:attribute>
>                         </xsl:element>
>                       </xsl:for-each>
>                     </xsl:element>
>                   </xsl:for-each-group>
>                 </xsl:element>
>               </xsl:for-each-group>
>             </xsl:element>
>           </xsl:for-each-group>
>         </xsl:element>
>       </xsl:for-each-group>
>     </xsl:element>
>   </xsl:template>
> </xsl:stylesheet>


Current Thread
Keywords