wmfox3
wmfox3

Reputation: 2221

parsing xml with lxml

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"?

Upvotes: 2

Views: 290

Answers (1)

Lance Helsten
Lance Helsten

Reputation: 10047

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.

Upvotes: 4

Related Questions