Skip to content

Instantly share code, notes, and snippets.

@sashahart
Created December 20, 2016 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sashahart/bcf6d1e2cd04389416507d7af29357f3 to your computer and use it in GitHub Desktop.
Save sashahart/bcf6d1e2cd04389416507d7af29357f3 to your computer and use it in GitHub Desktop.
"""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