[lxml-dev] prefix mappings
Hi, I would like to generate the following serialization: <a xmls="foo"> <b xmls="bar"/> </a> Currently, the closest I can come is: <a xmls="foo" xmlns:x="bar"> <x:b/> </a> using the following code: a = Element("{foo}a", nsmap={None:"foo", "x":"bar"}) a.append(Element("{bar}b")) Note that even though these serializations are equivalent, sometimes remapping namespaces is neccessary to work around faulty implementations in other products. The feed validator for example warns against using namespace prefixes on elements for good reason. If you don't, many readers aren't able to parse your feed (http://www.intertwingly.net/wiki/pie/XmlNamespaceConformanceTests). If you're using xhtml as content this means remapping the default namespace. In order to solve this I think it would be a good idea to allow (or take into account) prefix mappings on non root nodes as well. The output I'd like could then be achieved by the following code snippet: a = Element("{foo}a", nsmap={None:"foo"}) a.append(Element("{bar}b", nsmap={None:"bar"})) What do you think? Regards, Nick
FnH wrote:
I would like to generate the following serialization:
<a xmls="foo"> <b xmls="bar"/> </a> [...] In order to solve this I think it would be a good idea to allow (or take into account) prefix mappings on non root nodes as well. The output I'd like could then be achieved by the following code snippet:
a = Element("{foo}a", nsmap={None:"foo"}) a.append(Element("{bar}b", nsmap={None:"bar"}))
from lxml.etree import Element, tostring
a = Element("{foo}a", nsmap={None:"foo"}) a.append(Element("{bar}b", nsmap={None:"bar"}))
print tostring(a, pretty_print=True) <a xmlns="foo"> <b xmlns="bar"/> </a>
This is on lxml 2.0 alpha, lxml 1.3 should work alike. Stefan
participants (2)
-
FnH
-
Stefan Behnel