使用 XPath,如何处理包含冒号字符的属性?
Using XPath, how are attributes that contain a colon character processed?
给定以下 XML(片段):
<node id="b071f9fa-14b0-4217-8e97-eb41da73f598" type="Group" ext:score="90">
<node id="b071f9fa-14b0-4217-8e97-eb41da73f599" type="Person" ext:score="100">
<node id="b071f9fa-14b0-4217-8e97-eb41da73f600" type="Business" ext:score="80">
我想检索 ext:score
为 100 的节点的 id
。
当前代码:
match = dom.xpath('//node[@ext:score="100"]/@id')[0]
Returns一个例外:
lxml.etree.XPathEvalError: Undefined namespace prefix
我已经读过(在此处和 XPath 文档中)ext
首先需要定义为有效的命名空间,因为 DOM 如果包含 特殊个字符。但是,我一直无法找到一个很好的例子来说明如何做到这一点。我正在处理的摘录中没有 ext
的定义,我不确定如何创建名称空间 prefix
.
有什么想法吗?
XML 属性(或元素)名称(例如 ext:score
中的冒号字符将名称空间前缀 ext
与本地名称 score
分隔开。命名空间前缀本身仅在与命名空间值相关联时才有意义。
为此XML,
<metadata xmlns:ext="http://musicbrainz.org/ns/mmd-2.0#">
<node id="b071f9fa-14b0-4217-8e97-eb41da73f598" type="Group" ext:score="90">
<node id="b071f9fa-14b0-4217-8e97-eb41da73f599" type="Person" ext:score="100">
<node id="b071f9fa-14b0-4217-8e97-eb41da73f600" type="Business" ext:score="80">
</metadata>
这个 XPath,
//node[@ext:score="100"]/@id
将 select 具有 ext:score
属性值 100
的所有 node
元素的 id
属性,前提是一种将名称空间前缀 (ext
) 绑定到名称空间值 (http://musicbrainz.org/ns/mmd-2.0#
在调用 XPath 的语言或工具中的方法。
将命名空间前缀绑定到 Python 中的命名空间值(请参阅 了解 Python 和其他语言示例):
from lxml import etree
f = StringIO('your XML here')
doc = etree.parse(f)
r = doc.xpath('//node[@ext:score="100"]/@id',
namespaces={'ext':'http://musicbrainz.org/ns/ext#-2.0'})
请注意,如果您的 XML 使用 ext
而未声明,则不是 namespace-well-formed.
给定以下 XML(片段):
<node id="b071f9fa-14b0-4217-8e97-eb41da73f598" type="Group" ext:score="90">
<node id="b071f9fa-14b0-4217-8e97-eb41da73f599" type="Person" ext:score="100">
<node id="b071f9fa-14b0-4217-8e97-eb41da73f600" type="Business" ext:score="80">
我想检索 ext:score
为 100 的节点的 id
。
当前代码:
match = dom.xpath('//node[@ext:score="100"]/@id')[0]
Returns一个例外:
lxml.etree.XPathEvalError: Undefined namespace prefix
我已经读过(在此处和 XPath 文档中)ext
首先需要定义为有效的命名空间,因为 DOM 如果包含 特殊个字符。但是,我一直无法找到一个很好的例子来说明如何做到这一点。我正在处理的摘录中没有 ext
的定义,我不确定如何创建名称空间 prefix
.
有什么想法吗?
XML 属性(或元素)名称(例如 ext:score
中的冒号字符将名称空间前缀 ext
与本地名称 score
分隔开。命名空间前缀本身仅在与命名空间值相关联时才有意义。
为此XML,
<metadata xmlns:ext="http://musicbrainz.org/ns/mmd-2.0#">
<node id="b071f9fa-14b0-4217-8e97-eb41da73f598" type="Group" ext:score="90">
<node id="b071f9fa-14b0-4217-8e97-eb41da73f599" type="Person" ext:score="100">
<node id="b071f9fa-14b0-4217-8e97-eb41da73f600" type="Business" ext:score="80">
</metadata>
这个 XPath,
//node[@ext:score="100"]/@id
将 select 具有 ext:score
属性值 100
的所有 node
元素的 id
属性,前提是一种将名称空间前缀 (ext
) 绑定到名称空间值 (http://musicbrainz.org/ns/mmd-2.0#
在调用 XPath 的语言或工具中的方法。
将命名空间前缀绑定到 Python 中的命名空间值(请参阅
from lxml import etree
f = StringIO('your XML here')
doc = etree.parse(f)
r = doc.xpath('//node[@ext:score="100"]/@id',
namespaces={'ext':'http://musicbrainz.org/ns/ext#-2.0'})
请注意,如果您的 XML 使用 ext
而未声明,则不是 namespace-well-formed.