命名空間

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('{}*') ])
#輸出:[]
相關文章
相關標籤/搜索