For this example I would like to get the first b element from every t (real xml might be more deeply nested). Unfortunately I am limited to xpath 1.0. My initial thought was something like //t//b[position()=1] but i can’t get it to work.
<t>
<a>
<b/>
<b/>
<b/>
</a>
</t>
<t>
<a>
<b/>
<b/>
<b/>
</a>
</t>
That’s almost right. First note that the predicate
[position()=1]is equivalent to simply[1], I’ll use the shorter form from now on. Now by definitionis shorthand for
so it will give you all
belements that are nested somewhere inside atand are the firstbchild of their respective parent elements. Thus givenyou would get b’s 1 and 3. If you only want the first
binside eachtthen you needGiven the example XML above this would return only the
<b attr="1"/>You can usually treat
.//xas equivalent todescendant::xbut this is one of the edge cases that shows up the subtle distinction between the two…