使用 python/ElementTree 和命名空间创建 xml 文档

发布于 2024-12-03 02:17:07 字数 1380 浏览 2 评论 0原文

我正在尝试创建一个内存中的 xml 文档,以便根的子节点都需要名称空间。

最终文档应如下所示:

<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>            

但是,当我打印出使用 ElementTree lib 创建的文档时,它看起来更像是这样:

<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>

以下是我创建文档的方式:

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

如何摆脱“ns0”最终 XML 中的部分,因此它看起来更像上面提到的第一个示例?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很酷又爱笑 2024-12-10 02:17:07

使用 xml.etree,您无法获得第一个示例中的确切输出,但您可以使用(全局)register_namespace() 函数使用比“ns0”“更好”的前缀。例如: ET.register_namespace('rev', 'http://schemas.example.com/search/query/2010/5/revision') 将确保输出看起来像 rev:FeedEntity

然而,(兼容的)lxml 库在命名空间前缀方面更加灵活,并且 允许您在创建元素时提供前缀映射

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文