i need to build xml file using special name of items, this is my current code :
from lxml import etree
import lxml
from lxml.builder import E
wp = E.wp
tmp = wp("title")
print(etree.tostring(tmp))
current output is this :
b'<wp>title</wp>'
i want to be :
b'<wp:title>title</title:wp>'
how i can create items with name like this : wp:title
?
You confused the namespace prefix
wp
with the tag name. The namespace prefix is a document-local name for a namespace URI.wp:title
requires a parser to look for axmlns:wp="..."
attribute to find the namespace itself (usually a URL but any globally unique string would do), either on the tag itself or on a parent tag. This connects tags to a unique value without making tag names too verbose to type out or read.You need to provide the namepace, and optionally, the namespace mapping (mapping short names to full namespace names) to the element maker object. The default
E
object provided doesn't have a namespace or namespace map set. I'm going to assume that here thatwp
is thehttp://wordpress.org/export/1.2/
Wordpress namespace, as that seems the most likely, although it could also be that you are trying to send Windows Phone notifications.Instead of using the default
E
element maker, create your ownElementMaker
instance and pass it anamespace
argument to telllxml
what URL the element belongs to. To get the right prefix on your element names, you also need to give it ansmap
dictionary that maps prefixes to URLs:This produces a tag with both the correct prefix, and the
xmlns:wp
attribute:You can omit the
nsmap
value, but then you'd want to have such a map on a parent element of the document. In that case, you probably want to make separateElementMaker
objects for each namespace you need to support, and you put thensmap
namespace mapping on the outer-most element. When writing out the document,lxml
then uses the short names throughout.For example, creating a Wordpress WXR format document would require a number of namespaces:
and then you'd construct a document with
which, when pretty printed with
etree.tostring(doc, pretty_print=True, encoding="unicode")
, produces:Note how only the root
<rss>
element hasxmlns
attributes, and how the<wp:wxr_version>
tag uses the right prefix even though we only gave it the namespace URI.To give a different example, if you are building a Windows Phone tile notification, it'd be simpler. After all, there is just a single namespace to use:
which produces
Only the outer-most element,
<wp:Notification>
, now has thexmlns:wp
attribute. All other elements only need to include thewp:
prefix.Note that the prefix used is entirely up to you and even optional. It is the namespace URI that is the real key to uniquely identifying elements across different XML documents. If you used
E = ElementMaker(namespace="WPNotification", nsmap={None: "WPNotification"})
instead, and so produced a top-level element with<Notification xmlns="WPNotification">
you still have a perfectly legal XML document that, according to the XML standard, has the exact same meaning.