Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Is it possible to remove all namespace prefixes #931

Answered by tefra
GilesNicholas asked this question in Q&A
Discussion options

In relation to this discussion (#922), is it possible to remove all namespace prefixes?
e.g. what i have now is something similar to
<ns0:foo xmlns:ns0="http..."> <ns0:bar>test</ns0:bar> <ns1:baz>test2</ns1:baz> </ns0:foo>

What I'm trying to produce is:
<foo xmlns="http..."> <bar>test</bar> <baz>test2</baz> </foo>

The ns_map passed into the XmlSerializer.render method is a dict, and the key is the namespace, which means I can at most remove only one namespace with something like the below.

xml_serializer.render( dataclass_obj, ns_map={'': 'foo'} )

Is there a way to accomplish this?

You must be logged in to vote

There is no way to know from a top element the namespaces of child elements, unless we do multiple passes, which will slow down the serializer a lot. Not even lxml can do this automatically, but lxml provides https://lxml.de/apidoc/lxml.etree.html#lxml.etree.cleanup_namespaces

You could override https://github.com/tefra/xsdata/blob/main/xsdata/formats/dataclass/serializers/writers/lxml.py in order cleanup the namespaces of the generated self.handler.etree

There is no easy way to integrate that in the xsdata default writers.

Replies: 1 comment

Comment options

There is no way to know from a top element the namespaces of child elements, unless we do multiple passes, which will slow down the serializer a lot. Not even lxml can do this automatically, but lxml provides https://lxml.de/apidoc/lxml.etree.html#lxml.etree.cleanup_namespaces

You could override https://github.com/tefra/xsdata/blob/main/xsdata/formats/dataclass/serializers/writers/lxml.py in order cleanup the namespaces of the generated self.handler.etree

There is no easy way to integrate that in the xsdata default writers.

You must be logged in to vote
0 replies
Answer selected by tefra
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /