xsl-list
[Top] [All Lists]

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

2004-09-25 01:49:01
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[(_at_)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(_dot_)aerts(_at_)xml4pharma(_dot_)com 
[mailto:jozef(_dot_)aerts(_at_)xml4pharma(_dot_)com] 
Sent: 25 September 2004 09:34
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
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[(_at_)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[(_at_)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[(_at_)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[(_at_)itemcount = 4]">
                    <xsl:element name="D">
                      <xsl:attribute name="Value"><xsl:value-of
select="current-grouping-key()"/></xsl:attribute>
                      <xsl:for-each select="item[(_at_)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>

--+------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--+--




<Prev in Thread] Current Thread [Next in Thread>