Re: [xsl] Difficulty over understanding preceding-sibling

Subject: Re: [xsl] Difficulty over understanding preceding-sibling
From: "Joe Fawcett" <joefawcett@xxxxxxxxxxx>
Date: Sat, 3 Sep 2005 01:38:47 +0100
Thanks, I was trying to get around using node-set() on an RTF.

----- Original Message ----- From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Saturday, September 03, 2005 1:18 AM
Subject: Re: [xsl] Difficulty over understanding preceding-sibling

On 9/3/05, Joe Fawcett <joefawcett@xxxxxxxxxxx> wrote:
Can anyone explain why the first item being passed to the template has any
 <item id="1" />
 <item id="2" />
 <item id="3" />
 <item id="4" />
 <item id="5" />
 <item id="6" />
 <item id="7" />
 <item id="8" />
 <item id="9" />
 <item id="10" />

<xsl:stylesheet version="1.0" xmlns:xsl="";>
<xsl:param name="groupSize" select="3" />
<xsl:template match="/">
<xsl:call-template name="groupItems">
<xsl:with-param name="items" select="root/item" />

 <xsl:template name="groupItems">
   <xsl:param name="items" />
   <xsl:variable name="itemCount" select="count($items)" />
   <xsl:if test="$itemCount > 0">
       <xsl:for-each select="$items">
         <xsl:if test="position() <= $groupSize">
           <newItem id="{@id}">
             <xsl:if test="position() = 1">
               <xsl:attribute name="preceding-sibling-count">
                 <xsl:value-of select="count(preceding-sibling::item)" />
     <xsl:call-template name="groupItems">
       <xsl:with-param name="items" select="$items[position() > $groupSize]"

Result (msxml version 4):
 <group itemCount="10">
 <newItem id="1" preceding-sibling-count="0"/>
 <newItem id="2"/>
 <newItem id="3"/>
 <group itemCount="7">
 <newItem id="4" preceding-sibling-count="3"/>
 <newItem id="5"/>
 <newItem id="6"/>
 <group itemCount="4">
 <newItem id="7" preceding-sibling-count="6"/>
 <newItem id="8"/>
 <newItem id="9"/>
 <group itemCount="1">
 <newItem id="10" preceding-sibling-count="9"/>

Why doesn't the preceding-sibling-count show zero, or am I just up too late?

Because this is exactly the number of preceding siblings of the current node.

The "preceding-sibling" axis' scope is the current document, not a
dynamically defined node-list.

Most probably you want something else -- not the count of preceding siblings?

Dimitre Novatchev

Current Thread