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.

Joe
----- 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
preceding-sibling?
XML:
<root>
 <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" />
</root>

XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:param name="groupSize" select="3" />
<xsl:template match="/">
<new>
<xsl:call-template name="groupItems">
<xsl:with-param name="items" select="root/item" />
</xsl:call-template>
</new>
</xsl:template>


 <xsl:template name="groupItems">
   <xsl:param name="items" />
   <xsl:variable name="itemCount" select="count($items)" />
   <xsl:if test="$itemCount > 0">
     <group>
       <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:attribute>
             </xsl:if>
           </newItem>
         </xsl:if>
       </xsl:for-each>
     </group>
     <xsl:call-template name="groupItems">
       <xsl:with-param name="items" select="$items[position() > $groupSize]"
/>
     </xsl:call-template>
   </xsl:if>
 </xsl:template>
</xsl:stylesheet>

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

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?

Cheers,
Dimitre Novatchev

Current Thread