I'm new to XPath and am working with an XML file which looks like this:
<doc>
<component>
<author> Bob </author>
</component>
<component>
<sB>
<component>
<section ID='S1'>
<title>Some s1 title</title>
</section>
</component>
<component>
<section ID='S2'>
<title>Some s2 title</title>
</section>
</component>
</sB>
</component>
</doc>
I want to retrieve the component item above with section ID = S1, or alternatively the one that has a title element with text 'Some s1 title'. I cannot count on these things being in a particular order.
So far I've tried
import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
res = tree.getroot().findall(".//*[title='Some s1 title']../../")
for i in res:
ET.dump(i)
but that gets me both components, not just the one with the matching title.
I've also tried to search at the section ID level, like so:
res = tree.getroot().findall(".//*section[@ID='S1']/../")
for i in res:
ET.dump(i)
but that doesn't get me the parent (the whole component) and instead just gets me the section.
Both of these seem like they might work from the simple example syntax I've seen online, but clearly in both cases I'm missing some understanding of what is actually happening. Could someone please clarify what is happening here and why I'm not getting what I would expect?