How do I generate and parse XML like the following using lxml?
<s:Envelope xmlns:s="a" xmlns:a="http_//www.w3.org/2005/08/addressing">
....
</s:Envelope>
I currently swap : with _ in the element names when I parse and generate XML, but it seems stupid.
It’s not clear exactly what you’re asking, but maybe this will help:
An element such as
<s:Envelope>is using a XML namespace prefix. This is used to indicate that thes:Envelopeattribute in this document is defined in theanamespace.LXML represents XML namespaces using a namespace prefix in braces, for example:
{a}Envelope. Your example document is sort of confusing, because you also defined thea:namespace prefix, so:a:Elementis equivalent to{http://www.w3.org/2005/08/addressing}Element, ands:Elementis equivalent to{a}Element.Many of the LXML commands let you provide a namespace prefix mapping. For example, to find the
Envelopeelement in your document using XPATH, you could do this:Note that this is exactly equivalent to:
That is, the namespace prefix doesn’t have to match what is used in the source XML document; only the the absolute namespace matters.
You can read more about LXML and namespaces here.