我如何使用QName(python xml.etree.ElementTree?) [英] How do I use QName (python xml.etree.ElementTree?)

查看:98
本文介绍了我如何使用QName(python xml.etree.ElementTree?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对QName进行了大量的阅读,但是找不到如何使用它的良好示例。有人可以给我一个简单的示例,说明如何使用QName并解释将在什么上下文中使用它吗?

I have done a fair amount of reading on QName but I can't find any good examples of how to use it. Could someone give me a simple example of how to use QName and explain what context it would be used in?

推荐答案

<$ c在构造属性与包含元素不同的名称空间中的XML文档时,可以使用$ c> QName 。示例(Python 2.7):

QName can be used when constructing XML documents with attributes that are in a different namespace than the containing element. Example (Python 2.7):

from xml.etree import ElementTree as ET

NS1 = "http://example1.com" 
NS2 = "http://example2.com"

ET.register_namespace("x", NS1) 
ET.register_namespace("y", NS2)

qname1 = ET.QName(NS1, "root")    # Element QName 
qname2 = ET.QName(NS2, "attr")    # Attribute QName

root = ET.Element(qname1, {qname2: "test"}) 
print ET.tostring(root)

输出:

<x:root xmlns:x="http://example1.com" xmlns:y="http://example2.com" y:attr="test" />

一个可能有用的应用程序是 XLink

One application for which this can be useful is XLink.

这篇关于我如何使用QName(python xml.etree.ElementTree?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆