Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


XPath position predicates

From: Tom Anderson <twic@------.-----.-->
To: NULL
Date: 5/6/2009 5:35:00 PM
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

-- 
I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth. -- Umberto Eco


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent