Created
December 20, 2016 00:18
-
-
Save sashahart/bcf6d1e2cd04389416507d7af29357f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""An example of how to deal with XML namespaces using LXML. | |
""" | |
blob = b""" | |
<p:doc xmlns:p="http://my.de/fault/namespace"> | |
<p:title>The dog and the hog</p:title> | |
<p:section> | |
<p:title>The dog</p:title> | |
<p:par>Once upon a time, ...</p:par> | |
<p:par>And then ...</p:par> | |
</p:section> | |
<p:section> | |
<p:title>The hog</p:title> | |
<p:par>Sooner or later ...</p:par> | |
</p:section> | |
</p:doc> | |
""" | |
from lxml import etree | |
from io import BytesIO | |
fake_file = BytesIO(blob) | |
doc = etree.parse(fake_file) | |
# or directly etree.fromstring(blob) | |
# but you're probably going to read this xml from a file | |
title1 = doc.xpath( | |
"//p:title", | |
namespaces={ | |
"p": "http://my.de/fault/namespace" | |
} | |
) | |
assert len(title1) > 0 | |
prefix = "p" | |
namespace = "http://my.de/fault/namespace" | |
title2 = doc.xpath( | |
"//{}:title".format(prefix), | |
namespaces={ | |
prefix: namespace | |
} | |
) | |
assert title2 == title1 | |
title3 = etree.ETXPath("//{http://my.de/fault/namespace}title")(doc) | |
assert title1 == title3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment