[xsl] For-each-group groups elements before the first group-starting-with element

Subject: [xsl] For-each-group groups elements before the first group-starting-with element
From: Peter Desjardins <peter.desjardins.us@xxxxxxxxx>
Date: Mon, 10 May 2010 22:50:22 -0400
Hi. I'm new to XSLT and I'm trying to write a 2.0 stylesheet to wrap
HTML elements in DocBook <section> elements.  I'm finding that
<xsl:for-each-group> wraps elements in a <section> even when they are
not preceded by the element I name in the group-starting-with
attribute.

For example, I'm converting the HTML <body> element to a DocBook
<chapter> element. If the HTML includes <p> elements before the first
<h1> element, they should become <para> elements that are children of
the <chapter> element. I name <h1> in the group-starting-with
attribute but my output includes a <section> element around the first
<para> elements.

My processor is Saxon HE 9.2.1.1. I've pasted the source, stylesheet,
and output below. Am I using the for-each-group element incorrectly? I
figured out a way to work around this problem by including a predicate
[preceding-sibling::h1] in the select attribute for it.  However, this
won't work when I need to wrap <h2> and <h3> sections later in the
document.

Has anyone dealt with this situation before?  Thanks for your help.

Peter

***********  Source  ***********

<?xml version="1.0" encoding="UTF-8"?>
<html>
    <body>
        <p>Acorn</p>
        <p>Bee</p>
        <h1>1 Heading One</h1>
        <p>Caterpillar</p>
        <p>Dragonfly</p>
    </body>
</html>

***********  Stylesheet  ***********

<xsl:template match = "body" >
    <xsl:for-each-group select = "*" group-starting-with = "h1" >
        <xsl:element name="section">
            <xsl:apply-templates select="current-group()"/>
        </xsl:element>
    </xsl:for-each-group>
</xsl:template>

***********  Output  ***********

<?xml version="1.0" encoding="UTF-8"?>
<book>
   <chapter>
      <section>
         <para>Acorn</para>
         <para>Bee</para>
      </section>
      <section>
         <title>1 Heading One</title>
         <para>Caterpillar</para>
         <para>Dragonfly</para>
      </section>
   </chapter>
</book>

Current Thread