[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

current-group() question

Subject: current-group() question
From: Ruud Grosmann <r.grosmann@xxxxxx>
Date: Thu, 06 Dec 2007 14:28:56 +0100
 current-group() question
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.