Page 1 of 1

xpath question

Posted: Fri Feb 24, 2006 10:52 pm
by w0mbat
here's my xml:

Code: Select all

<xml>
<base>
<left x="y"></left>
<middle></middle>
<right>
<left x="y"></left>
<middle></middle>
<right x="y"></right>
</right>
</base>
</xml>
returns three nodes: two "left" nodes and one "right" node.
/xml//*[@x='y']

returns the two "left" nodes:
/xml//*[@x='y'][1]

returns the "right" node:
/xml//*[@x='y'][2]

returns nothing:
/xml//*[@x='y'][3]


Is there a way i can structure the first query (that returns all three nodes) in such a way that appending the position predicates [1],[2],[3] results in three queries that each uniquely identify a single node?

thanks!
-w

Posted: Sat Feb 25, 2006 1:24 am
by w0mbat
nevermind, a tall glass of good beer helped me divine the answer: wrap the original query in parentheses, and this will cause a new node-set to be created prior to the position predicate being evaluated:

(/xml//*[@x='y'])[1]

-w

Posted: Wed Mar 01, 2006 5:21 pm
by george
Hi,

A side note to your post: it is bad practice to use the names starting with xml as you did. It is not an error but these are reserved names, see
http://www.w3.org/TR/2004/REC-xml-20040 ... common-syn
Names beginning with the string "xml", or with any string which would match (('X'|'x') ('M'|'m') ('L'|'l')), are reserved for standardization in this or future versions of this specification.
Best Regards,
George