xsl-list
[Top] [All Lists]

RE: [xsl] Recursive positional grouping (was xsl digest...)

2007-04-05 01:08:51
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(_dot_)jacks(_at_)yahoo(_dot_)co(_dot_)uk] 
Sent: 05 April 2007 08:28
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] 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(_at_)saxonica(_dot_)com>
Subject: RE: [xsl] Recursive positional grouping (was xsl digest...)
Message-ID: <009e01c7769e$d165d290$6401a8c0(_at_)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 

--~------------------------------------------------------------------
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>
--~--



--~------------------------------------------------------------------
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>