Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating xml doc with python/ElementTree and namespaces

I'm trying to create an in-memory xml document such that the root's child nodes all require a name space.

The final document should look something like this:

<Feed>            
<FeedEntity Id="0000" 
      xmlns="http://schemas.example.com/search/query/2010/5/revision">

    <FeedRequest locale="en-US" title="<some value>"/>
</FeedEntity>
... another FeedEntity element ...
</Feed>            

However, when I print out the document I've created with ElementTree lib, it looks more like this:

<Feed>
    <ns0:FeedEntity Id="0000"
      xmlns:ns0="http://schemas.example.com/search/query/2010/5/revision">

        <FeedRequest locale="en-US" title="<some value>"/>
    </ns0:FeedEntity>
</Feed>

Here's how I'm creating the document:

counter = 0
namespace = "http://schemas.example.com/search/query/2010/5/revision"

root = Element("Feed")        

node_name = "{%s}FeedEntity" % (namespace, );                
feed_entity_element = Element(node_name)        

feed_entity_element["Id"] = "%04d" % (counter,);

feed_request_element = Element("FeedRequest");
feed_request_element["Culture"] = self.culture;
feed_request_element["Query"] = address;        

# append each of the elements to the xml document 
feed_entity_element.append(feed_request_element);

root.append(feed_entity_element);

str_data = ET.tostring(root)
print str_data

How do I get rid of the "ns0" parts in the final XML so it looks more like the first example noted above?

like image 772
Dan Holman Avatar asked Sep 05 '11 20:09

Dan Holman


People also ask

How do you create an XML structure in Python?

Creating XML Document using Python First, we import minidom for using xml. dom . Then we create the root element and append it to the XML. After that creating a child product of parent namely Geeks for Geeks.

How do you create a namespace in XML?

XML Namespaces - The xmlns Attribute When using prefixes in XML, a namespace for the prefix must be defined. The namespace can be defined by an xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".

What is XML Etree ElementTree in Python?

The xml.etree.ElementTree module implements a simple and efficient API for parsing and creating XML data. Changed in version 3.3: This module will use a fast implementation whenever available.


1 Answers

With xml.etree, you cannot get the exact output as in the first example, but you can use the (global) register_namespace() function to use a "better" prefix than "ns0". For example: ET.register_namespace('rev', 'http://schemas.example.com/search/query/2010/5/revision') will make sure the output will look like rev:FeedEntity.

The (compatible) lxml library, however, is more flexible with regard to namespace prefixes, and allows you to provide a prefix mapping when creating an element.

like image 79
Steven Avatar answered Nov 01 '22 20:11

Steven