Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: XPath position predicates

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 5/6/2009 6:46:00 PM
Tom Anderson wrote:

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

-- 

	Martin Honnen
	http://msmvps.com/blogs/martin_honnen/


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