xsl-list
[Top] [All Lists]

RE: [xsl] Determining the position of a specific node in the context

2006-10-20 02:34:17

The task: I want to determine if in the current context, a 
node with certain properties comes before another.

Example:

<root>
  <A/>
  <B/>
  <C/>
</root>

I want to know whether <B/> comes before <C/>. 

I think you actually want to know whether <B/> comes before <C/> in the
source tree, not in the current context. You can construct a context in
which <C/> precedes <B/>, for example

<xsl:for-each select="*">
  <xsl:sort select="name()" order="descending"/>

but I don't think that's the question you are asking (if it is, you've got
problems).

So forget about the position of the nodes in the context, and think about
the position of the nodes in the source tree. You've only given one example
input, and it's hard to infer the general problem from one example, but you
can do

test="B/following-sibling::C"

or in 2.0 you can do

test="$C >> $B".



[Side note: Testing the above in Saxon 8 (XSLT2, XPath2) does 
not give an error or warning, but the test simply evaluates 
to false. So, "node() [self::B]/position()" must be a legal 
XPath 2 expression - what does it actually do?]

X/position() says "for each node in X, return its position in the list". So
it means the same as the expression

1 to count(X)

For some reason "1 to count(X)" is a much more common idiom, but since
X/position() is fewer characters, I expect David Carlisle will change
that...

Michael Kay
http://www.saxonica.com/


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