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