命名空间

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

 

建立带前缀和命名空间的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的方式
这样就能够无需使用前缀,使得代码更加清晰和准确spa

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


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

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

# 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名blog

# 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属性查找为节点定义的当前命名空间
请注意,这包括在元素上下文中已知的全部前缀,而不单单是它本身定义的那些前缀ip

# 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节)不认为未固定的属性名在名称空间中
因此它们最终可能会在序列化解析往返过程当中丢失名称空间,即便它们出如今名称空间元素中utf-8

 

# 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('{}*') ])
#输出:[]
相关文章
相关标签/搜索