xsl-list
[Top] [All Lists]

Re: Difficulty over understanding preceding-sibling

2005-09-02 17:18:27
On 9/3/05, Joe Fawcett <joefawcett(_at_)hotmail(_dot_)com> 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="{(_at_)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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>