This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: using position() with XPath
- To: <xsl-list at lists dot mulberrytech dot com>
- Subject: RE: [xsl] using position() with XPath
- From: "Michael Kay" <mhkay at iclway dot co dot uk>
- Date: Tue, 11 Sep 2001 14:52:04 +0100
- Reply-To: xsl-list at lists dot mulberrytech dot com
The position() function (which takes no arguments) returns the position of
the context node in the context node list, that is the list of nodes
selected by the most recent call of xsl:apply-templates or xsl:for-each.
To find the position of a particular node within the source tree, use
count(). For example, count(ancestor::parentMenu/preceding-sibling::*)+1.
Alternatively, use <xsl:number>.
Mike Kay
> -----Original Message-----
> From: owner-xsl-list@lists.mulberrytech.com
> [mailto:owner-xsl-list@lists.mulberrytech.com]On Behalf Of Michael
> Sobczak
> Sent: 11 September 2001 14:05
> To: XSL-List@lists.mulberrytech.com
> Subject: [xsl] using position() with XPath
>
>
> Hi,
>
> I need to use position() to get the position of
> an ancestor to the current <childMenu> node.
> Using the following xml:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <menus>
> <parentMenu type="upper" title="Main Menu">
> <childMenus>
> <childMenu title="News"/>
> <childMenu title="Discussions"/>
> </childMenus>
> </parentMenu>
> <parentMenu type="lower" title="International
> Menu">
> <childMenus>
> <childMenu title="Distribution"/>
> <childMenu title="World Headquarters"/>
> </childMenus>
> </parentMenu>
> </menus>
>
> I created the the following template for the
> <childMenu> node:
>
> <xsl:template match="childMenu[@title]">
> <LI><xsl:value-of
> select="position(ancestor::parentMenu)"/>_<xsl:value-of
> select="position()"/>. <xsl:value-of
> select="@title"/></LI>
> <xsl:apply-templates/>
> </xsl:template>
>
> However, both of the position() calls return the
> same value, like so:
>
> 1. Main Menu
> 1_1. News
> 2_2. Discussions
>
> 2. International Menu
> 1_1. Distribution
> 2_2. World Headquarters
>
> When I change the first value-of in the template
> to use "1 +
> count(parent::*/preceding-sibling::*)" as
> described in the FAQs, I get this:
>
> 1. Main Menu
> 1_1. News
> 1_2. Discussions
>
> 2. International Menu
> 1_1. Distribution
> 1_2. World Headquarters
>
> What do I need to do to determine the position of
> the current node's ancestor?
>
>
> Thank you for your help,
>
> - Mike.
>
> __________________________________________________
> Do You Yahoo!?
> Get email alerts & NEW webcam video instant messaging with
> Yahoo! Messenger
> http://im.yahoo.com
>
> XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list