[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] group-adjacent problem


Subject: [xsl] group-adjacent problem
From: Ruud Grosmann <r.grosmann@xxxxxx>
Date: Tue, 16 Dec 2008 13:44:06 +0100

hi group,

I want to solve a very simple problem using xslt2 and saxon9, but I can't get my style sheet doing what I want it to.

My goal is to group the subelements of consequetive sect-elements into a single sections element:

<doc>
<chapter>
<sect>
<p>p1</p>
<p>p2</p>
</sect>
<sect>
<p>p3</p>
</sect>
<image/>
<sect>
<p>p4</p>
</sect>
</chapter>
</doc>

should transform into

<doc>
<chapter>
<sections>
<p>p1</p>
<p>p2</p>
<p>p3</p>
</sections>
<image/>
<sections>
<p>p4</p>
</sections>
</chapter>
</doc>

This my stylesheet:

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


  <xsl:output method="xml" version="1.0" encoding="utf-8"
   indent="yes" />

  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match='*[sect]'>
    <xsl:for-each-group select="*" group-adjacent="name()" >
      <xsl:for-each select="current-group()">
        <xsl:choose>
          <xsl:when test="current-grouping-key() = 'sect'">
            <sections>
              <xsl:apply-templates select='.' mode='sect'/>
            </sections>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select='.'/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </xsl:for-each-group>
  </xsl:template>

  <xsl:template match='sect' mode='sect'>
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match='*'>
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

However, the stylesheet doesn't group and produces instead:

<?xml version="1.0" encoding="utf-8"?>
<doc>
   <sections>
      <p>p1</p>
      <p>p2</p>
   </sections>
   <sections>
      <p>p3</p>
   </sections>
   <image/>
   <sections>
      <p>p4</p>
   </sections>
</doc>

The first to sect elements apparently didn't end up into the same group.

What do I do wrong?

Thanks in advance!

Ruud


Current Thread