XPath position predicates

Tom Anderson

Usenet Archive
Joined
Aug 22, 2003
Messages
2,716
Reaction score
0
Hello everybody,

This may be a stupid question, but i've been banging my brain against it all afternoon, and as such i'm no longer think good.

Given this XML:

<table id="mytable">
<tr>
<td class="left">port</td>
<td class="right">starboard</td>
</tr>
<tr>
<td class="left">gauche</td>
<td class="right">droite</td>
</tr>
<tr>
<td class="left">sinister</td>
<td class="right">dexter</td>
</tr>
</table>

(a) Should this XPath expression find the cell containing 'starboard':

table[@id='mytable']/tr[1]/td[@class='right']

(b) Should these XPath expressions (i) mean the same thing and (ii) find the cell containing 'starboard':

(1) table[@id='mytable']/tr/td[@class='right'][1]
(2) table[@id='mytable']//td[@class='right'][1]
(3) table[@id='mytable']//td[@class='right' and position=1]

?

I wrote some code on the assumption that the answer to (b)(ii) was 'yes', but it didn't work. I'm not sure if this is a mistaken assumption on my part, a failure to translate a correct assumption into correct code, or a bug in my XPath library (Xalan 2.7.1).

My thinking had been that if you have an expression E that identifies a node set, then E[n] selects the nth node of that set. But fiddling and reading, it dawned on me that this isn't the case, but rather that E[n] means the members of E which are the nth child of their parents - in this case, the tr elements. That would be fine, but what i found rather counterintuitive is that example (b)(2), which is the form of expression i was using, contains no explicit mention of any tr, and so that means you're indexing into completely mysterious parent elements!

In conclusion, sometimes i love XPath, and sometimes i hate it.

Thanks,
tom
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
Tom said:
This may be a stupid question, but i've been banging my brain against it all afternoon, and as such i'm no longer think good.

Given this XML:

<table id="mytable">
<tr>
<td class="left">port</td>
<td class="right">starboard</td>
</tr>
<tr>
<td class="left">gauche</td>
<td class="right">droite</td>
</tr>
<tr>
<td class="left">sinister</td>
<td class="right">dexter</td>
</tr>
</table>

(a) Should this XPath expression find the cell containing 'starboard':

table[@id='mytable']/tr[1]/td[@class='right']
Yes.

(b) Should these XPath expressions (i) mean the same thing and (ii) find the cell containing 'starboard':

(1) table[@id='mytable']/tr/td[@class='right'][1]
(2) table[@id='mytable']//td[@class='right'][1]
(3) table[@id='mytable']//td[@class='right' and position=1]

?

I wrote some code on the assumption that the answer to (b)(ii) was 'yes', but it didn't work. I'm not sure if this is a mistaken assumption on my part, a failure to translate a correct assumption into correct code, or a bug in my XPath library (Xalan 2.7.1).

My thinking had been that if you have an expression E that identifies a node set, then E[n] selects the nth node of that set.

I think you want
(E)[n]
so e.g.
(table[@id='mytable']/tr/td[@class='right'])[1]
 

Tom Anderson

Usenet Archive
Joined
Aug 22, 2003
Messages
2,716
Reaction score
0
Tom said:
My thinking had been that if you have an expression E that identifies a node set, then E[n] selects the nth node of that set.

I think you want
(E)[n]
so e.g.
(table[@id='mytable']/tr/td[@class='right'])[1]

I finally got round to testing this, and of course you're absolutely right. Thanks!

tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,050
Messages
3,104,249
Members
248,133
Latest member
DujonT

Latest Threads

Top