Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 19th, 2007, 11:50 AM
Registered User
 
Join Date: Apr 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Position of a node outside current context

Is it possible to find the position of a node outside of the current context? For instance:

<ROOT>
  <PARENTTYPE1>
    <CHILD id="id1"/>
    <CHILD id="id2"/>
  </PARENTTYPE1>
  <PARENTTYPE2>
    <CHILD id="id3"/>
    <CHILD id="id4"/>
  </PARENTTYPE2>
</ROOT>

<xsl:template name="findPositionOfChild">
  <xsl:param name="idOfChild"/>
  <xsl:for-each select="//CHILD">
    <xsl:if test="@id=$idOfChild">
      <xsl:value-of select="position()"/>
    </xsl:if>
  </xsl:for-each>
</xsl:template>

If I call the above template with <xsl:with-param name="idOfChild">id3</xsl:with-param> the result of the position() call will be "3". I assume that this is because the position of the child with id="id3" in the node set returned by <xsl:for-each select="//CHILD"> is 3. But I want to find the position of the child with id="id3" in the context of that child's parent (it's the 1st child of the parent <PARENTTYPE2>).

I assume this can be done with an xpath expression similar to the following:
<xsl:variable name="id3Var">id3</xsl:variable>
<xsl:variable name="id3Pos" select="//*[@id=$id3Var].position()"/>
But the syntax is incorrect.
 
Old April 19th, 2007, 12:33 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The position() function returns the position of a node within the sequence of nodes currently being processed. This has nothing to do with its position in the source tree.

To find out the position of a node in the source tree, use xsl:number or count(preceding-sibling::*), or suitable variations.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 19th, 2007, 02:07 PM
Registered User
 
Join Date: Apr 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ah, I was under the impression that preceding-sibling::* worked the same way as position() did. i.e that it applies to the nodes being processed instead of the source tree.

Thanks






Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting the position of a node in the original XML zvikico XSLT 8 July 17th, 2008 09:31 AM
Please help to check the current node... darshil XSLT 1 May 9th, 2007 02:42 AM
"for-the-current-node" instead of "for-each" ? webhead XSLT 2 August 25th, 2006 02:55 PM
xpath: position parent of current node orion83 XSLT 1 September 5th, 2005 07:20 AM
Understanding context node in templates Birger XSLT 3 August 31st, 2004 04:21 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.