You can use xpath
to achieve the purpose. Following Python code works perfect:
import libxml2
data = """
<bundles>
<bundle>
<bitstreams>
<bitstream>
<id>1234</id>
</bitstream>
</bitstreams>
<name>FOO</name>
</bundle>
</bundles>
"""
doc = xmllib2.parseDoc(data)
for node in doc.xpathEval('/bundles/bundle/name[.="FOO"]/../bitstreams/bitstream/id'):
print node
or using lxml
(data
is the same as in the example above):
from lxml import etree
bundles = etree.fromstring(data)
for node in bundles.xpath('bundle/name[.="FOO"]/../bitstreams/bitstream/id'):
print(node.text)
outputs:
1234
If the <bitstreams>
element always precedes the <name>
element, you can also use the more efficient xpath expression:
'bundle/name[.="FOO"]/preceding-sibling::bitstreams/bitstream/id'