Reputation: 6933
Lets suppose it has this:
xml_as_str = '''
<v1:Header>
<v2:Person>Foo Bar</v2:Person>
<v2:Email>[email protected]</v2:Email>
</v1:Header>
'''
from lxml import etree
tree = etree.fromstring(xml_as_str, etree.XMLParser(recover=True))
How could it get the value of certain tag, e.g "Foo Bar" for v2:Person ?
I've tried this:
>> tree.find('.//v2:Person')
>> tree.find('.//{Person}v2')
in order to get the element and then .text
, but .find
doesn't find the element, why?.
Upvotes: 2
Views: 418
Reputation: 174696
You could use BeautifulSoup also.
In [1]: from bs4 import BeautifulSoup
In [2]: xml_as_str = '''
...: <v1:Header>
...: <v2:Person>Foo Bar</v2:Person>
...: <v2:Email>[email protected]</v2:Email>
...: </v1:Header>
...: '''
In [9]: soup = BeautifulSoup(xml_as_str, 'lxml')
In [15]: for i in soup.find_all('v1:header'):
...: for j in soup.find_all('v2:person'):
...: print(j.text)
...:
Foo Bar
In [16]:
Through List-Comprehension.
In [17]: [j.text for i in soup.find_all('v1:header') for j in soup.find_all('v2:person')][0]
Out[17]: 'Foo Bar'
Upvotes: 0
Reputation: 368894
Using local-name()
xpath function:
>>> xml_as_str = '''
... <v1:Header>
... <v2:Person>Foo Bar</v2:Person>
... <v2:Email>[email protected]</v2:Email>
... </v1:Header>
... '''
>>>
>>> from lxml import etree
>>> tree = etree.fromstring(xml_as_str, etree.XMLParser(recover=True))
>>> tree.xpath('//*[local-name()="v2:Person"]/text()')[0]
'Foo Bar'
Upvotes: 2