Subject: RE: Recursive positional grouping (was xsl digest...)
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 5 Apr 2007 09:08:21 +0100
|
Within xsl:for-each-group, current-group() selects all the nodes in the
group being processed, whereas "." selects only the first node in that
group. Writing "." is equivalent to writing current-group()[1].
Michael Kay
http://www.saxonica.com/
> -----Original Message-----
> From: john jacks [mailto:john.jacks@xxxxxxxxxxx]
> Sent: 05 April 2007 08:28
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: RE: Recursive positional grouping (was xsl digest...)
>
> Thanks Michael.
> (As Abel pointed out, I'm replying to the digest since that's
> what I receive).
>
> This works nicely. I don't understand the difference between
> <xsl:apply-templates select="."/>
>
> and
>
> <xsl:apply-templates select="current-group()"/>
>
> within the xsl:for-each-group instruction.
>
> The book says ' use current-group() is sometimes better
> especially if the group includes elements of different types'
> which this example does.
>
> How can I understand the difference please?
>
> JJ
>
>
> From: "Michael Kay" <mike@xxxxxxxxxxxx>
> Subject: RE: Recursive positional grouping (was xsl digest...)
> Message-ID: <009e01c7769e$d165d290$6401a8c0@turtle>
>
> I think your problem is that
>
> <xsl:apply-templates select="current-group()"/>
>
> is processing all the elements in a group, not only the
> next-level headings
> - so the paragraphs are output at every level.
>
> I think this kind of recursive grouping works best if you use
> a named template that is passed the contents of a group as a
> parameter. Try something like this:
>
> <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet
> version="2.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> xmlns:xs="http://www.w3.org/2001/XMLSchema";>
> <xsl:output indent="yes"/>
>
> <xsl:template name="group">
> <xsl:param name="pop" as="element()*"/>
> <xsl:param name="level" as="xs:integer" />
> <xsl:variable name="key" as="xs:string" select="concat('h',
> $level)"/>
> <xsl:for-each-group select="$pop"
> group-starting-with="*[name() eq $key]">
> <xsl:choose>
> <xsl:when test="starts-with(name(.), 'h')">
> <section level="{$level}">
> <head><xsl:apply-templates
> select="current-group()[1]"/></head>
> <xsl:call-template name="group">
> <xsl:with-param name="pop"
> select="remove(current-group(), 1)"/>
> <xsl:with-param name="level" select="$level + 1"/>
> </xsl:call-template>
> </section>
> </xsl:when>
> <xsl:otherwise>
> <xsl:copy-of select="current-group()"/>
> </xsl:otherwise>
> </xsl:choose>
> </xsl:for-each-group>
> </xsl:template>
>
> <xsl:template match="/">
> <doc>
> <xsl:call-template name="group">
> <xsl:with-param name="pop" select="body/*"/>
> <xsl:with-param name="level" select="1"/>
> </xsl:call-template>
> </doc>
> </xsl:template>
>
> </xsl:stylesheet>
>
>
>
>
>
>
>
>
>
> ___________________________________________________________
> New Yahoo! Mail is the ultimate force in competitive
> emailing. Find out more at the Yahoo! Mail Championships.
> Plus: play games and win prizes.
> http://uk.rd.yahoo.com/evt=44106/*http://mail.yahoo.net/uk
|