[lxml-dev] Best Way To Strip A Namespace

Hello, Say I have the following document: <box xmlns="http://example.com/box" xmlns:foo="http://example.com/foo"> <foo:bar>baz></foo:bar> </box> After I have parsed it into an element tree, how do I change it in a fool proof manner to loose the root namespace so it becomes: <box xmlns:foo="http://example.com/foo"> <foo:bar>baz></foo:bar> </box> This is important for processing my DocBook sources as the XSLT stylesheets cannot handle the namepace on the root element. Thanks, Noah -- "Creativity can be a social contribution, but only in so far as society is free to use the results." - R. Stallman

Hi Noah, Noah Slater wrote:
Say I have the following document:
<box xmlns="http://example.com/box" xmlns:foo="http://example.com/foo"> <foo:bar>baz></foo:bar> </box>
After I have parsed it into an element tree, how do I change it in a fool proof manner to loose the root namespace so it becomes:
<box xmlns:foo="http://example.com/foo"> <foo:bar>baz></foo:bar> </box>
Have you tried setting the tag name to the local name (without namespace)? Stefan

Hi Stefan, This does not seem to work, instead it is changing the local name but leaves the namespace alone. I have attached an example for you to look at. Thanks, Noah On 28/05/06, Stefan Behnel <behnel_ml@gkec.informatik.tu-darmstadt.de> wrote:
Hi Noah,
Noah Slater wrote:
Say I have the following document:
<box xmlns="http://example.com/box" xmlns:foo="http://example.com/foo"> <foo:bar>baz></foo:bar> </box>
After I have parsed it into an element tree, how do I change it in a fool proof manner to loose the root namespace so it becomes:
<box xmlns:foo="http://example.com/foo"> <foo:bar>baz></foo:bar> </box>
Have you tried setting the tag name to the local name (without namespace)?
Stefan
-- "Creativity can be a social contribution, but only in so far as society is free to use the results." - R. Stallman

Hi Noah, Noah Slater wrote:
On 28/05/06, Stefan Behnel wrote:
Noah Slater wrote:
Say I have the following document:
<box xmlns="http://example.com/box" xmlns:foo="http://example.com/foo"> <foo:bar>baz></foo:bar> </box>
After I have parsed it into an element tree, how do I change it in a fool proof manner to loose the root namespace so it becomes:
<box xmlns:foo="http://example.com/foo"> <foo:bar>baz></foo:bar> </box>
Have you tried setting the tag name to the local name (without namespace)?
This does not seem to work, instead it is changing the local name but leaves the namespace alone. I have attached an example for you to look at.
#!/usr/bin/env python
import sys
from lxml import etree
root_name = 'article' namespace_uri = 'http://docbook.org/ns/docbook'
namespace_map = {None: 'http://docbook.org/ns/docbook'}
root_element = etree.Element("{%s}%s" % (namespace_uri, root_name), nsmap=namespace_map)
# Stefan, this is what I interpreted from your email: root_element.tag = 'essay'
element_tree = etree.ElementTree(root_element)
element_tree.write(sys.stdout) sys.stdout.write('\n')
Ok, I tried this and it failed. Nice. I fixed this, so lxml now removes the namespace reference from the element. However, there is more involved than you might think, as it still leaves the namespace in the document. Finding out if the namespace is still in use is rather expensive... Anyway, the problem itself is fixed on the trunk. Stefan
participants (2)
-
Noah Slater
-
Stefan Behnel