Tuesday, November 08, 2011


Using lxml xpath to get elements with a default namespace


xml_string="""
<a xmlns="http://www.w3.org/1999/xhtml">
  <b>
    <r>
      <Value>string</Value>
    </r>
    <r>
      <Value>string</Value>
    </r>
  </b>
  <e>
    <string>string1</string>
    <string>string2</string>
  </e>
</a>
"""

from lxml import etree
xml = etree.fromstring(xml_string)
xml.xpath('/w3:a/w3:b/w3:r/w3:Value/text()', namespaces={'w3':'http://www.w3.org/1999/xhtml'})

No comments: