[xsl] current-group() question

Subject: [xsl] current-group() question
From: Ruud Grosmann <r.grosmann@xxxxxx>
Date: Thu, 06 Dec 2007 14:28:56 +0100
hello group,

while I was playing with for-each-group (saxon8), I found strange differences between my test style sheets I couldn't explain. I hope you can help me understanding the reason why. I've made the following minimal example of it.

XML ======================

<root>
<p>paragraph 1.</p>
<p>paragraph 2.</p>
<p>paragraph 3.</p>
<b>no paragraph.</b>
<p>paragraph 4.</p>
<p>paragraph 5.</p>
<b>no paragraph.</b>
</root>

style sheet #1 ===============================================

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


<xsl:output indent='yes'/>

  <xsl:template match="/">
    <xsl:apply-templates select="." mode='list'/>
  </xsl:template>

  <xsl:template match="root" mode='list'>
    <xsl:copy>
      <xsl:for-each-group select="*"
                          group-adjacent="boolean(self::p)">
        <xsl:choose>
          <xsl:when test="current-group()/p">
            <p-block>
              <xsl:for-each select="current-group()">
                <xsl:apply-templates select='.' mode='list'/>
              </xsl:for-each>
            </p-block>
          </xsl:when>
          <xsl:otherwise>
            <no-p-block>
              <xsl:for-each select="current-group()">
                <xsl:apply-templates select='.' mode='list'/>
              </xsl:for-each>
            </no-p-block>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*" mode='list'>
    <xsl:copy>
      <xsl:apply-templates mode='list'/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

output stylesheet #1 ========================================

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <no-p-block>
      <p>paragraph 1.</p>
      <p>paragraph 2.</p>
      <p>paragraph 3.</p>
   </no-p-block>
   <no-p-block>
      <b>no paragraph.</b>
   </no-p-block>
   <no-p-block>
      <p>paragraph 4.</p>
      <p>paragraph 5.</p>
   </no-p-block>
   <no-p-block>
      <b>no paragraph.</b>
   </no-p-block>
</root>


I would expect the first paragraphs to be enclosed in a p-block element like my second stylesheet does:


style sheet #2=========================

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


<xsl:output indent='yes'/>

  <xsl:template match="/">
    <xsl:apply-templates select="." mode='list'/>
  </xsl:template>

  <xsl:template match="root" mode='list'>
    <xsl:copy>
      <xsl:for-each-group select="*"
                          group-adjacent="boolean(self::p)">
        <xsl:variable name='curgroup'>
          <xsl:sequence select='current-group()'/>
        </xsl:variable>

        <xsl:choose>
          <xsl:when test="$curgroup/p">
            <p-block>
              <xsl:for-each select="$curgroup">
                <xsl:apply-templates select='.' mode='list'/>
              </xsl:for-each>
            </p-block>
          </xsl:when>
          <xsl:otherwise>
            <no-p-block>
              <xsl:for-each select="$curgroup">
                <xsl:apply-templates select='.' mode='list'/>
              </xsl:for-each>
            </no-p-block>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*" mode='list'>
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

output style sheet #2 ====================

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <p-block>
      <p>paragraph 1.</p>
      <p>paragraph 2.</p>
      <p>paragraph 3.</p>
   </p-block>
   <no-p-block>
      <b>no paragraph.</b>
   </no-p-block>
   <p-block>
      <p>paragraph 4.</p>
      <p>paragraph 5.</p>
   </p-block>
   <no-p-block>
      <b>no paragraph.</b>
   </no-p-block>
</root>


Note that the only difference is the use of variable curgroup. I don't understand why the two stylesheet have a different behaviour. Can you explain?


thanks in advance, Ruud

Current Thread