David542
David542

Reputation: 110073

How to find element attribute using lxml

Suppose I have the the following xml:

<package xmlns="http://example/namespace">
    <rating system="au-oflc">PG</rating>
    ...
</package>

To get the text of an element in the above, I am doing the following:

from lxml import entree
f = open('/Users/David/Desktop/metadata.xml')
metadata_contents = f.read()
node = etree.fromstring(metadata_contents)
rating = node.xpath('//t:rating/text()', namespaces = {'t':'http://example/namespace'})
>>> rating
['PG']

How would I get the value "au-oflc" ?

Upvotes: 6

Views: 16566

Answers (3)

jcomeau_ictx
jcomeau_ictx

Reputation: 38412

georg's answer assumes all rating elements will have a system tag. if that is not necessarily the case, using rating[0].attrib.get('system') will avoid a KeyError.

Upvotes: 5

unutbu
unutbu

Reputation: 879073

You can also access the attribute using XPath:

system = node.xpath('//t:rating/@system', namespaces = {'t':'http://example/namespace'})
print system[0]

Upvotes: 2

georg
georg

Reputation: 214949

You need to retrieve the node itself, not its text:

rating = node.xpath('//t:rating', namespaces = {'t':'http://example/namespace'})
print rating[0].attrib['system']

Upvotes: 16

Related Questions