命名空间

时间:2019-11-13
本文章向大家介绍命名空间,主要包括命名空间使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

ElementTree API尽可能避免使用前缀,而是使用命名空间(URI)

创建带前缀和命名空间的xml树

# coding:utf-8
from lxml import etree

#当使用命名空间http://www.w3.org/1999/xhtml时,会自动创建前缀html,并让其与之关联,
#如果URL错误,如http://www.w3.org/1999/xhtml33333,则会创建前缀ns0,即<ns0:html.......
xhtml = etree.Element("{http://www.w3.org/1999/xhtml}html") 
body = etree.SubElement(xhtml, "{http://www.w3.org/1999/xhtml}body")
body.text = "Hello World"

print(etree.tostring(xhtml, pretty_print=True))
 
'''
输出:
<html:html xmlns:html="http://www.w3.org/1999/xhtml"> 
  <html:body>Hello World</html:body>
</html:html>
'''

ElementTree使用的符号最初是由James Clark提出的
其主要优点就是,它提供了一种不管文档是否使用或定义了哪些前缀,都可以锁定树中任何一个Tag的方式
这样就可以无需使用前缀,使得代码更加清晰和准确

从示例中可以看到,前缀仅在序列化结果时才变得重要


如果命名空间名称过长,比如:http://www.w3.org/1999/xhtml......,
这样代码在变得冗长同时,代码中反复地重新键入或复制字符串也很容易出错
因此,常见的做法是将命名空间存储在全局变量中

要调整命名空间前缀以进行序列化,还可向Element factory函数传递映射,例如定义默认命名空间

# coding:utf-8
from lxml import etree

XHTML_NAMESPACE = "http://www.w3.org/1999/xhtml"
XHTML = "{%s}" % XHTML_NAMESPACE

NSMAP = {'prefix_test' : XHTML_NAMESPACE} # the default namespace (no prefix)

xhtml = etree.Element(XHTML + "html", nsmap=NSMAP) # lxml only!
body = etree.SubElement(xhtml, XHTML + "body")
body.text = "Hello World"

print(etree.tostring(xhtml, pretty_print=True))

'''
输出:
<prefix_test:html xmlns:prefix_test="http://www.w3.org/1999/xhtml">
  <prefix_test:body>Hello World</prefix_test:body>
</prefix_test:html>
'''

使用QName助手类生成或拆分限定tag名

# coding:utf-8
from lxml import etree

tag = etree.QName('http://www.w3.org/1999/xhtml', 'html')
print(tag.localname) #输出:html
print(tag.namespace) #输出:http://www.w3.org/1999/xhtml
print(tag.text) #输出:{http://www.w3.org/1999/xhtml}html

tag = etree.QName('{http://www.w3.org/1999/xhtml}html')
print(tag.localname) #输出:html
print(tag.namespace) #输出:http://www.w3.org/1999/xhtml

root = etree.Element('{http://www.w3.org/1999/xhtml}html')
tag = etree.QName(root)
print(tag.localname) #输出:html

tag = etree.QName(root, 'script')
print(tag.text)  #输出:{http://www.w3.org/1999/xhtml}script

tag = etree.QName('{http://www.w3.org/1999/xhtml}html', 'script')
print(tag.text) #输出:{http://www.w3.org/1999/xhtml}script

nsmap属性
etree允许通过.nsmap属性查找为节点定义的当前命名空间
请注意,这包括在元素上下文中已知的所有前缀,而不仅仅是它自己定义的那些前缀

# coding:utf-8
from lxml import etree

root = etree.Element('root', nsmap={'a': 'http://a.b/c'})
child = etree.SubElement(root, 'child',nsmap={'b': 'http://b.c/d'})

print(root.nsmap) #输出:{'a': 'http://a.b/c'}
print(child.nsmap) #输出:{'b': 'http://b.c/d'}
print(len(root.nsmap)) #输出:1
print(len(child.nsmap)) #输出:2
print(child.nsmap['a'])  #输出:'http://a.b/c'
print(child.nsmap['b'])  #输出:'http://b.c/d'

修改返回的dict不会对元素产生任何有意义的影响,对它的任何更改都将被忽略
属性上的命名空间工作原理类似,但从2.3版开始,lxml.etree将确保属性使用带前缀的命名空间声明
这是因为XML名称空间规范(第6.2节)不认为未固定的属性名在名称空间中
所以它们最终可能会在序列化解析往返过程中丢失名称空间,即使它们出现在名称空间元素中

# coding:utf-8
from lxml import etree

XHTML_NAMESPACE = "http://www.w3.org/1999/xhtml"
XHTML = "{%s}" % XHTML_NAMESPACE

NSMAP = {'prefix_test' : XHTML_NAMESPACE} # the default namespace (no prefix)

xhtml = etree.Element(XHTML + "html", nsmap=NSMAP) # lxml only!
body = etree.SubElement(xhtml, XHTML + "body")
body.text = "Hello World"

body.set(XHTML + "bgcolor", "#CCFFAA")
print(etree.tostring(xhtml, pretty_print=True))

'''
输出:
<prefix_test:html xmlns:prefix_test="http://www.w3.org/1999/xhtml">
  <prefix_test:body prefix_test:bgcolor="#CCFFAA">Hello World</prefix_test:body>
</prefix_test:html>
'''

print(body.get("bgcolor")) #输出:None
print(body.get(XHTML + "bgcolor")) #输出:'#CCFFAA'


#还可将XPath与完全限定名一起使用
find_xhtml_body = etree.ETXPath("//{%s}body" % XHTML_NAMESPACE)
results = find_xhtml_body(xhtml)
print(results[0].tag)  #输出:{http://www.w3.org/1999/xhtml}body


#为了方便起见,可在lxml.etree的所有迭代器中使用“*”通配符,包括标记名和名称空间
for el in xhtml.iter('*'): 
    print(el.tag)   # any element
'''
输出:
{http://www.w3.org/1999/xhtml}html
{http://www.w3.org/1999/xhtml}body
'''

for el in xhtml.iter('{http://www.w3.org/1999/xhtml}*'): 
    print(el.tag)
'''
输出:
{http://www.w3.org/1999/xhtml}html
{http://www.w3.org/1999/xhtml}body
'''

for el in xhtml.iter('{*}body'): 
    print(el.tag)
'''
输出:
{http://www.w3.org/1999/xhtml}body
'''

#若要查找没有命名空间的元素,请使用纯标记名或显式提供空命名空间
print([ el.tag for el in xhtml.iter('{http://www.w3.org/1999/xhtml}body') ])
#输出:['{http://www.w3.org/1999/xhtml}body']

print([ el.tag for el in xhtml.iter('body') ])
#输出:[]

print([ el.tag for el in xhtml.iter('{}body') ])
#输出:[]

print([ el.tag for el in xhtml.iter('{}*') ])
#输出:[]

原文地址:https://www.cnblogs.com/shiliye/p/11850010.html