Consider this XML structure, a dumbed-down version of the DDEX standard:
<doc>
<master>
<ResourceInfo>
<Name>Foo</Name>
<Seq>1</Seq>
</ResourceInfo>
<ResourceInfo>
<Name>Bar</Name>
<Seq>2</Seq>
</ResourceInfo>
</master>
<track>
<Resource>
<Name>Foo</Name>
</Resource>
</track>
<track>
<Resource>
<Name>Bar</Name>
</Resource>
</track>
</doc>
I'd like to select the ResourceInfo node in <master>
with a child <Name>
matching the text value of the Name of each of the track node to get the Seq number.
I can do so directly by getting an lxml tree of each track and explicitly requesting <ResourceInfo>
's like this:
track.xpath('/doc/master/ResourceInfo/Seq[../Name[text()="Foo"]]')
But that assumes I know the name of each track and can explicitly state it ahead of time. I'd like to be able to dumbly map this and somehow replace the "Foo" in the xpath with some reference to the Name text()
of current track's Resource.
It's kind of like joining tracks and resources on the text()
of the Name in master with the text()
of Name in each track. Is there an easy way of doing this with XPath?
I'm trying to iterate over each track, and pull the Seq from the track. Therefore, I can't explicitly ask for "Foo". I need to introspect - "Give me the Seq that is a sibling of a <Name>
node in master with a value matching <Name>
of the current node in <track>
".