xsl-list
[Top] [All Lists]

Re: XPatch: ancestor and count

2004-10-01 02:27:59
Hi Andrey,

1         - root element
2    1    - root, menu
3    2    - root, menu, menuitem "2"
5    3    - root, menu, menuitem "3", menuitem "3.4" and what???
and menuitem "3.6"?

   <xsl:for-each select="child::menuitem">
    <tr><td><xsl:value-of select="count(.//ancestor::*)"/></td><td>
    <xsl:value-of select="@title"/></td></tr>
   </xsl:for-each>
In this context, './/' means 'descendant-or-self::*', right? (I'm not sure, the use of '//' still confuses me a bit). But it explains the output:

menuitem 1:
- descendant-or-self::* contains 1 node.
- that node has 2 ancestors (at least, if the <menu> element is the child of a root element): root and menu.

menuitem 2:
- descendant-or-self::* contains 2 nodes (menuitem 2 and 2.1).
- that set has 3 ancestors: root, menu and menuitem 2

menuitem 3:
- descendant-or-self::* contains 11 nodes (menuitem 3 and all its descendants). - count(.//ancestor::*) counts the ancestors within these 11 nodes, and that includes menuitem 3.6 (because that one has children too) so that's 5

Cheers,
Anton

Andrey V. Elsukov wrote:

Hi,

I have some XML file, for example:
=======
<menu>
     <menuitem title="1"/>
     <menuitem title="2">
       <menuitem title="2.1"/>
     </menuitem>
     <menuitem title="3">
       <menuitem title="3.1"/>
       <menuitem title="3.2"/>
       <menuitem title="3.3"/>
       <menuitem title="3.4">
             <menuitem title="3.4.1"/>
             <menuitem title="3.4.2"/>
       </menuitem>
       <menuitem title="3.5"/>
       <menuitem title="3.6">
             <menuitem title="3.6.1"/>
             <menuitem title="3.6.2"/>
       </menuitem>
     </menuitem>
</menu>
======
and XLS transform:
======
<xsl:template match="menu">
 <table class="menu">
   <tr><td><xsl:value-of select="count(ancestor::*)"/></td><td>&#xA0;</td></tr>
   <xsl:for-each select="child::menuitem">
    <tr><td><xsl:value-of select="count(.//ancestor::*)"/></td><td>
    <xsl:value-of select="@title"/></td></tr>
   </xsl:for-each>
 </table>
</xsl:template>
======
In result the following:
======
1         - root element
2    1    - root, menu
3    2    - root, menu, menuitem "2"
5    3    - root, menu, menuitem "3", menuitem "3.4" and what???
======
http://www.w3.org/TR/1999/REC-xpath-19991116 :
* the ancestor axis contains the ancestors of the context node;
the ancestors of the context node consist of the parent of context
node and the parent's parent and so on;..

* Function: number count(node-set)
The count function returns the number of nodes in the argument node-set.
======
Why the result is 5? It should be equal 4, or not?

PS. I use Perl-5.8.5 + LibXML + LibXSLT..
--
Best regards,
Andrey V. Elsukov


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