xsl-list
[Top] [All Lists]

Re: [xsl] position() from the attribute axis for getting the position of the parent in respect to its siblings

2007-01-18 05:58:07
Andrew Welch wrote:
position() is always based on the nodes position within the currently
selected node list, not its position within the XML document.

David Carlisle wrote:
I'm not sure why, but this seems to be a common misunderstanding about
position(). position() has _no_ relationship to the position of a node
in a source document, it just relates to the position of a node in the
current node list (in xslt 1) or the current item in the current
sequence (in xslt2).

Michael Kay wrote:
No, it returns the position in whatever set of nodes you are currently
processing. Those might be siblings, or they might not.

Thank you all for the clear replies. Somehow, position() backfires at me every now and then and I get lost in trying things like the following, which, I might add, sounds entirely logical to my ever unexperienced eyes (but doesn't act as I expect):

some/path[selection]/position()

Which, I now understand, will *always* return "1" if the preceding xpath selects one node, or a sequence of consecutive numbers (from 1 up, I presume), if the preceding xpath selects several nodes.



David Carlisle wrote:
I'm not sure what number you want, probably <xsl:number/> or count(preceding-sibling::*)+1

Yes indeed. These were solution 0 and 1 in the example I send.


David Carlisle wrote:
http://www.dpawson.co.uk/xsl/sect2/N6099.html#d8191e311

I misunderstood the phrase "in the context of some node list". And it all went a bit blurry when I read on through the other articles. But now the clouds clear up.



Andrew Welch wrote:

So to get the result you're after you need to build a node list and
then get the position of the node you're after:

<xsl:attribute name="daynr4">
 <xsl:variable name="genID" select="generate-id(parent::*)"/>
 <xsl:for-each select="../../day">
         ^^^^^^^^^^^^^^ This builds the node list
   <xsl:if test="generate-id() = $genID">
     <xsl:value-of select="position()"/>
   </xsl:if>
 </xsl:for-each>
</xsl:attribute>

The above is of course a waste of time, you should just use 1 +
count(parent::*/preceding-sibling::*))

I understand it is a waste of time, but it does help me in understanding the subject, thanks. However, in lieu of this code, can you explain to me why a for ... in ... return ... loop does not work the same way and always returns one? I.e., the following returns a sequence of ones, instead of the expected 1 2 3:

for $elem in ../../day return position()

Or should I read this as the "position of the current element in the current context, which is the same as current()/position and not the position that $elem takes in the set of nodes ../../day".


Kind regards,
-- Abel Braaksma
  http://www.nuntia.nl

--~------------------------------------------------------------------
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>
--~--