Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - XPath position predicates >Thread Next - Re: XPath position predicates Re: XPath position predicatesTo: 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/ | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
