2
votes

I've got an xml file with the following:

<content_item>
    <primary_keys>
        <primary_key>
            <source>firstone</source>
            <value>123</value>
        </primary_key>
        <primary_key>
            <source>secondone</source>
            <value>456</value>
        </primary_key>
    </primary_keys>
</content_item>

Content items can have one or more primary_key. What's the most efficient way using lxml to get the value for the primary_key with a source of "firstone" and the value for the primary_key source of "secondone"?

1

1 Answers

4
votes

Use the XPath module in lxml. For "firstone" the abbreviated syntax XPath would be //primary_key[source="firstone"]/value/text() and for "second one" the abbreviated syntax XPath would be //primary_key[source="second one"]/value/text().

Use http://www.xpathtester.com/test to try it out.