Group-starting-with problem in XSLT 2.0

Hi folks!

I am using XSLT 2.0 with Altova XML Spy 2005 professional edition;
which have the inbuilt processor for XSLT 2.0; to transform one XML
file into other XML file.

I've one XML file that contains a large amount of data with lots of
tags but without nesting/hierarchies.

For example:

<book>
      <Story>

<h1>chapter1</h1>
              <h3>article1</h3>
              <text>text text</text>
              <h2>heading1</h2>
              <text>text text</text>
              <h3>article1</h3>
              <text>text text</text>
              <h2>heading2</h2>
              <text>text text</text>
              <h3>article1</h3>
              <text>text text</text>

<h1>chapter2</h1>
              <h2>heading1</h2>
              <text>text text</text>
              <h3>article1</h3>
              <text>text text</text>

      </Story>
</book>

And XSLT file is like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2004/07/xpath-functions"
xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes">

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

<xsl:template match="Story">
 <xsl:for-each-group select="node()" group-starting-with="h1">
   <chapter>
                      <xsl:call-template name="h2">
                               <xsl:with-param name="child.elements"
select="current-group()"></xsl:with-param>
                      </xsl:call-template>
   </chapter>
 </xsl:for-each-group>
</xsl:template>

<xsl:template name="h2">
<xsl:param name="child.elements"></xsl:param>
 <xsl:for-each-group select="$child.elements" group-starting-with="h2">
   <xsl:choose>
                      <xsl:when test="current-group()[1][self::h1]">
                              <xsl:apply-templates select="current-group()"/>
                      </xsl:when>
              <xsl:otherwise>
                      <heading>
                       <xsl:call-template name="h3">
                       <xsl:with-param name="child.elements"
select="current-group()"></xsl:with-param>
                       </xsl:call-template>
               </heading>
   </xsl:otherwise>
   </xsl:choose>
 </xsl:for-each-group>
</xsl:template>

<xsl:template name="h3">
<xsl:param name="child.elements"></xsl:param>
 <xsl:for-each-group select="$child.elements" group-starting-with="h3">
   <xsl:choose>
                      <xsl:when test="current-group()[1][self::h1 or
self::h2]">
                              <xsl:apply-templates select="current-group()"/>
                      </xsl:when>
              <xsl:otherwise>
   <article>
               <xsl:apply-templates  select="current-group()"/>
   </article>
   </xsl:otherwise>
   </xsl:choose>
 </xsl:for-each-group>
</xsl:template>

<xsl:template match="h1 | h2 | h3">
      <title><xsl:apply-templates /></title>
</xsl:template>

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

</xsl:stylesheet>

If the highlighted <h3>article1</h3> line is not in there in source
XML file, then it gives the absolutely correct result as I want.

For example:

<?xml version="1.0" encoding="UTF-8"?>
<book xmlns:fn="http://www.w3.org/2004/07/xpath-functions"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <chapter>
              <title>chapter1</title>
              <text>text text</text>
              <heading>
                      <title>heading1</title>
                      <text>text text</text>
                      <article>
                              <title>article1</title>
                              <text>text text</text>
                      </article>
              </heading>
              <heading>
                      <title>heading2</title>
                      <text>text text</text>
                      <article>
                              <title>article1</title>
                              <text>text text</text>
                      </article>
              </heading>
      </chapter>
      <chapter>
              <title>chapter2</title>
              <heading>
                      <title>heading1</title>
                      <text>text text</text>
                      <article>
                              <title>article1</title>
                              <text>text text</text>
                      </article>
              </heading>
      </chapter>
</book>

But if that <h3>article1</h3> line is there in XML file as shown
above, it gives me wrong result…

For example : (result with <h3>article1</h3> line in source XML file)

<?xml version="1.0" encoding="UTF-8"?>
<book xmlns:fn="http://www.w3.org/2004/07/xpath-functions"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <chapter>
              <title>chapter1</title>
              <title>article1</title>
              <text>text text</text>
              <heading>
                      <title>heading1</title>
                      <text>text text</text>
                      <article>
                              <title>article1</title>
                              <text>text text</text>
                      </article>
              </heading>
              <heading>
                      <title>heading2</title>
                      <text>text text</text>
                      <article>
                              <title>article1</title>
                              <text>text text</text>
                      </article>
              </heading>
      </chapter>
      <chapter>
              <title>chapter2</title>
              <heading>
                      <title>heading1</title>
                      <text>text text</text>
                      <article>
                              <title>article1</title>
                              <text>text text</text>
                      </article>
              </heading>
      </chapter>
</book>

It means, if all my source elements come in proper order it works
fine, but if any one of them is incorrectly or knowingly places in the
source XML file… its gives the in-correct result.

Is there anybody who can help me out?
---------------------------------------------------------
Thanks & regards,

Lalit K Sharma
Software Engineer
Rough Guides India (Pearson Group) http://www.roughguides.com
New Delhi, India
Official email Id: lalit.sharma@roughguides.com
Personal email Id/s: lalit_k_sharma@yahoo.com , lalitksharma@gmail.com

Received on Thursday, 21 April 2005 11:57:12 UTC