博客
关于我
命名空间
阅读量:502 次
发布时间:2019-03-07

本文共 3863 字,大约阅读时间需要 12 分钟。

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

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

# coding:utf-8from lxml import etree#当使用命名空间http://www.w3.org/1999/xhtml时,会自动创建前缀html,并让其与之关联,#如果URL错误,如http://www.w3.org/1999/xhtml33333,则会创建前缀ns0,即
Hello World
'''

ElementTree使用的符号最初是由James Clark提出的

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

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

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

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

# coding:utf-8from lxml import etreeXHTML_NAMESPACE = "http://www.w3.org/1999/xhtml"XHTML = "{%s}" % XHTML_NAMESPACENSMAP = {    '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))'''输出:
Hello World
'''

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

# coding:utf-8from lxml import etreetag = etree.QName('http://www.w3.org/1999/xhtml', 'html')print(tag.localname) #输出:htmlprint(tag.namespace) #输出:http://www.w3.org/1999/xhtmlprint(tag.text) #输出:{http://www.w3.org/1999/xhtml}htmltag = etree.QName('{http://www.w3.org/1999/xhtml}html')print(tag.localname) #输出:htmlprint(tag.namespace) #输出:http://www.w3.org/1999/xhtmlroot = etree.Element('{http://www.w3.org/1999/xhtml}html')tag = etree.QName(root)print(tag.localname) #输出:htmltag = etree.QName(root, 'script')print(tag.text)  #输出:{http://www.w3.org/1999/xhtml}scripttag = 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-8from lxml import etreeroot = 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)) #输出:1print(len(child.nsmap)) #输出:2print(child.nsmap['a'])  #输出:'http://a.b/c'print(child.nsmap['b'])  #输出:'http://b.c/d'#修改返回的dict不会对元素产生任何有意义的影响,对它的任何更改都将被忽略child.nsmap['b'] = 'test'print(child.nsmap['b'])  #输出:'http://b.c/d'

属性上的命名空间工作原理类似,但从2.3版开始,lxml.etree将确保属性使用带前缀的命名空间声明

这是因为XML名称空间规范(第6.2节)不认为未固定的属性名在名称空间中
所以它们最终可能会在序列化解析往返过程中丢失名称空间,即使它们出现在名称空间元素中

# coding:utf-8from lxml import etreeXHTML_NAMESPACE = "http://www.w3.org/1999/xhtml"XHTML = "{%s}" % XHTML_NAMESPACENSMAP = {    '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))'''输出:
Hello World
'''print(body.get("bgcolor")) #输出:Noneprint(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('{}*') ])#输出:[]

转载地址:http://ycicz.baihongyu.com/

你可能感兴趣的文章
MSP430F149学习之路——SPI
查看>>
msp430入门编程45
查看>>
MSSQL数据库查询优化(一)
查看>>
MSSQL数据库迁移到Oracle(二)
查看>>
MSSQL日期格式转换函数(使用CONVERT)
查看>>
MSTP多生成树协议(第二课)
查看>>
MSTP是什么?有哪些专有名词?
查看>>
Mstsc 远程桌面链接 And 网络映射
查看>>
Myeclipse常用快捷键
查看>>
MyEclipse更改项目名web发布名字不改问题
查看>>
MyEclipse用(JDBC)连接SQL出现的问题~
查看>>
mt-datetime-picker type="date" 时间格式 bug
查看>>
myeclipse的新建severlet不见解决方法
查看>>
MyEclipse设置当前行背景颜色、选中单词前景色、背景色
查看>>
Mtab书签导航程序 LinkStore/getIcon SQL注入漏洞复现
查看>>
myeclipse配置springmvc教程
查看>>
MyEclipse配置SVN
查看>>
MTCNN 人脸检测
查看>>
MyEcplise中SpringBoot怎样定制启动banner?
查看>>
MyPython
查看>>