Re: [xsl] Flat to hierarchical with for-each-group (XSLT 2.0 solution)

Subject: Re: [xsl] Flat to hierarchical with for-each-group (XSLT 2.0 solution)
From: JBryant@xxxxxxxxx
Date: Wed, 25 May 2005 12:05:37 -0500
Ah, so. I forgot the self axis again. I definitely have a mental block 
about that thing.

So the corrected stylesheet should be:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

  <xsl:template match="doc">
    <doc>
      <xsl:for-each-group select="*" group-starting-with="heading1">
        <heading1 title="{current-group()[1]}">
          <xsl:for-each-group select="current-group()" 
group-starting-with="heading2">
            <xsl:choose>
              <xsl:when test="current-group()[1]/self::heading2">
                <heading2 title="{current-group()[1]}">
                  <xsl:for-each-group select="current-group()" 
group-starting-with="heading3">
                    <xsl:choose>
                      <xsl:when test="current-group()[1]/self::heading3">
                        <heading3 title="{current-group()[1]}">
                          <xsl:apply-templates select="current-group()"/>
                        </heading3>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:apply-templates select="current-group()"/>
                      </xsl:otherwise>
                    </xsl:choose>
                  </xsl:for-each-group>
                </heading2>
              </xsl:when>
              <xsl:otherwise>
                <xsl:apply-templates select="current-group()"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:for-each-group>
        </heading1>
      </xsl:for-each-group>
    </doc>
  </xsl:template>

  <xsl:template match="heading1|heading2|heading3"/>

  <xsl:template match="para">
    <xsl:copy-of select="."/>
  </xsl:template>

</xsl:stylesheet>

(retested with Saxon 8.4 to be sure)

Thanks, David.

Jay Bryant
Bryant Communication Services
(presently consulting at Synergistic Solution Technologies)




David Carlisle <davidc@xxxxxxxxx> 
05/25/2005 11:38 AM
Please respond to
xsl-list@xxxxxxxxxxxxxxxxxxxxxx


To
xsl-list@xxxxxxxxxxxxxxxxxxxxxx
cc

Subject
Re: [xsl] Flat to hierarchical with for-each-group (XSLT 2.0 solution)








> The only mystery to me is why I had to use 
test="current-group()[1]/name() 
> = 'heading2'" 

It's best not to use name() (especially if there is any possibility of
namespaces being involved) as name() isn't guaranteed to return the
prefix that you expect, and you are not supposed to care about prefixes
anyway.
"current-group()[1]/self::heading2"
is your friend.


> rather than test="current-group()/heading2" 

that tests if any node in the current group has a heading2 child rather
than testing if it is a heading2.

> just test="heading2"

That tests if the current node has a heading2 child. 

David

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

Current Thread