Bdfy
Bdfy

Reputation: 24699

How do I use ":" in XML element names using lxml?

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.

Upvotes: 1

Views: 264

Answers (1)

larsks
larsks

Reputation: 312008

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 the s:Envelope attribute in this document is defined in the a namespace.

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 the a: namespace prefix, so:

  • a:Element is equivalent to {http://www.w3.org/2005/08/addressing}Element, and
  • s:Element is equivalent to {a}Element.

Many of the LXML commands let you provide a namespace prefix mapping. For example, to find the Envelope element in your document using XPATH, you could do this:

import lxml.etree as etree
doc = etree.parse('mydocument.xml')
envelope = doc.xpath('//s:Envelope',
  namespaces={'s': 'a'})

Note that this is exactly equivalent to:

envelope = doc.xpath('//x:Envelope',
  namespaces={'x': 'a'})

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.

Upvotes: 7

Related Questions