Using lxml.etree
directly (the same should apply to ElementTree
), you can achieve the result like this:
doc = lxml.etree.fromstring(...)
tag_elements = doc.xpath("/peers/peer/offset[text()='1']/../tag")
tag_elements
will be the list of <tag>
elements belonging to <peer>
elements containing an <offset>
element containing 1.
Given input (I've added a <peer>
clause to emphasize tag_elements
being a list):
<peers>
<peer>
<offset>1</offset>
<tag>TRUE</tag>
</peer>
<peer>
<offset>1</offset>
<tag>OTHER</tag>
</peer>
<peer>
<offset>2</offset>
<tag>FALSE</tag>
</peer>
</peers>
tag_elements
will contain two elements:
for tag in tag_elements:
print tag.text
-> TRUE
-> OTHER
UPDATE:
doc.xpath("/peers/peer[offset=1]/tag")
also works fine.
But doc.xpath("./peers/peer[offset=1]/tag")
does not.