Xpath expression to find non-child elements by attribute -
here's nice puzzle. suppose have bit of code:
<page n="1"> <line n="3">...</line> </page>
it real easy locate line element "n=3" within page element "n=1" simple xpath expression: xpath(//page[@n='1')/line[@n='3']). great, beautiful, elegant. suppose have encoding (folks familiar tei know coming from).
<pb n="1"/> (arbitrary amounts of stuff) <lb n="3"/>
we want find lb element n="3", follows pb element n="1". note -- lb element anywhere following pb: may not (and not) sibling, child of sibling of pb, or of pb's parent, etc etc etc.
so question: how search lb element n="3", follows pb element n="1", xpath?
thanks in advance
peter
use:
//pb[@n='1']/following::lb[@n='2'] | //pb[@n='1']/descendant::lb[@n='2']
this selects lb
element follows specified pb
in document order -- if wanted lb
element descendant of pb
element.
do note the following expression doesn't in general select wanted lb
elements (it fails select of these descendants of pb
element):
//pb[@n='1']/following::lb[@n='2']
explanation:
as defined in w3c xpath specification, following::
, descendant::
axes non-overlapping:
"the following axis contains nodes in same document context node after context node in document order, excluding descendants , excluding attribute nodes , namespace nodes"
Comments
Post a Comment