xpath question

Questions about XML that are not covered by the other forums should go here.
w0mbat
Posts: 2
Joined: Fri Feb 24, 2006 10:42 pm

xpath question

Post 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
w0mbat
Posts: 2
Joined: Fri Feb 24, 2006 10:42 pm

Post 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
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post 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
Post Reply