Re: [xsl] Grouping Adjacent Elements in XSLT 1.0 and Duplicates

Subject: Re: [xsl] Grouping Adjacent Elements in XSLT 1.0 and Duplicates
From: Anton Triest <anton@xxxxxxxx>
Date: Wed, 20 Oct 2004 23:17:04 +0200
Hi Joe,

You want to generate an <ol> for every group of <ol_li> elements so that would be

   <xsl:template match="ol_li">
       <xsl:if test="not(preceding-sibling::*[1][self::ol_li])">
           <ol>
               <xsl:apply-templates select="." mode="li"/>
           </ol>
       </xsl:if>
   </xsl:template>

<xsl:template match="ol_li" mode="li">
<li><xsl:apply-templates/></li>
<xsl:apply-templates select="following-sibling::*[1][self::ol_li]" mode="li"/>
</xsl:template>


Cheers,
Anton


Joe Heidenreich wrote:


For my translation, my input looks like this:

<para>text1</para>
<para>text2</para>
<ol_li>first ordered list element 1</ol_li>
<ol_li>first ordered list element 2</ol_li>
<ol_li>first ordered list element 3</ol_li>
<para>text3</para>
<para>text4</para>
<para>text5</para>
<ol_li>second ordered list element 1</ol_li>
<ol_li>second ordered list element 2</ol_li>

The output needs to look like this:

<p>text1</p>
<p>text2</p>
<ol>
    <li>first ordered list element 1</li>
    <li>first ordered list element 2</li>
    <li>first ordered list element 3</li>
</ol>
<p>text3</p>
<p>text4</p>
<p>text5</p>
<ol>
    <li>second ordered list element 1</li>
    <li>second ordered list element 2</li>
</ol>

The number of lists and items and paragraphs will vary.

Currently my stylesheet looks like this:

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

<xsl:template match="ol_li">
<p>
<ol>


Here's the problem: an <ol> is generated for every <ol_li> element

              <xsl:apply-templates select="." mode="list-item" />
         </ol>
    </p>
    <xsl:apply-templates select="following-sibling::node()[not(self::ol_li)][1]" />
</xsl:template>

<xsl:template match="ol_li" mode="list-item">
    <li>
         <xsl:apply-templates/>
    </li>
    <xsl:apply-templates mode="list-item" select="following-sibling::node()[1][self::ol_li]" />
</xsl:template>
...

But the output is yielding decrementing duplicates like this:

<p>
<ol>
<li>first ordered list element 1</li>
<li>first ordered list element 2</li>
<li>first ordered list element 3</li>
</ol>
</p>

<p>
<ol>
<li>first ordered list element 2</li>
<li>first ordered list element 3</li>
</ol>
</p>

<p>
<ol>
<li>first ordered list element 3</li>
</ol>
</p>

It seems to be a recursion problem but i would appreciate any help I could get. Thanks.
-Joe

Current Thread