从 lxml 选择属性值

发布于 2024-11-09 12:59:14 字数 916 浏览 0 评论 0原文

我想使用 xpath 表达式来获取属性的值。

我期望以下内容能够工作

from lxml import etree

for customer in etree.parse('file.xml').getroot().findall('BOB'):
    print customer.find('./@NAME')

,但这会产生错误:

Traceback (most recent call last):
  File "bob.py", line 22, in <module>
    print customer.find('./@ID')
  File "lxml.etree.pyx", line 1409, in lxml.etree._Element.find (src/lxml/lxml.etree.c:39972)
  File "/usr/local/lib/python2.7/dist-packages/lxml/_elementpath.py", line 272, in find
    it = iterfind(elem, path, namespaces)
  File "/usr/local/lib/python2.7/dist-packages/lxml/_elementpath.py", line 262, in iterfind
    selector = _build_path_iterator(path, namespaces)
  File "/usr/local/lib/python2.7/dist-packages/lxml/_elementpath.py", line 246, in _build_path_iterator
    selector.append(ops[token[0]](_next, token))
KeyError: '@'

我期望它能够工作是错误的吗?

I want to use an xpath expression to get the value of an attribute.

I expected the following to work

from lxml import etree

for customer in etree.parse('file.xml').getroot().findall('BOB'):
    print customer.find('./@NAME')

but this gives an error :

Traceback (most recent call last):
  File "bob.py", line 22, in <module>
    print customer.find('./@ID')
  File "lxml.etree.pyx", line 1409, in lxml.etree._Element.find (src/lxml/lxml.etree.c:39972)
  File "/usr/local/lib/python2.7/dist-packages/lxml/_elementpath.py", line 272, in find
    it = iterfind(elem, path, namespaces)
  File "/usr/local/lib/python2.7/dist-packages/lxml/_elementpath.py", line 262, in iterfind
    selector = _build_path_iterator(path, namespaces)
  File "/usr/local/lib/python2.7/dist-packages/lxml/_elementpath.py", line 246, in _build_path_iterator
    selector.append(ops[token[0]](_next, token))
KeyError: '@'

Am I wrong to expect this to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╄→承喏 2024-11-16 12:59:14

findfindall 仅实现XPath 的子集。它们的存在旨在提供与其他 ElementTree 实现(如 ElementTree 和 cElementTree )的兼容性。

相比之下,xpath 方法提供对 XPath 1.0 的完全访问:

print customer.xpath('./@NAME')[0]

但是,您也可以使用 get:

print customer.get('NAME')

attrib

print customer.attrib['NAME']

find and findall only implement a subset of XPath. Their presence is meant to provide compatibility with other ElementTree implementations (like ElementTree and cElementTree).

The xpath method, in contrast, provides full access to XPath 1.0:

print customer.xpath('./@NAME')[0]

However, you could instead use get:

print customer.get('NAME')

or attrib:

print customer.attrib['NAME']
江南月 2024-11-16 12:59:14

作为一个可能有用的补充,这是在元素具有多个属性的情况下如何获取属性的值,并且这是相对于另一个元素的唯一区别。
例如,给定以下 file.xml:

<?xml version ="1.0" encoding="UTF-8"?>
    <level1>
      <level2 first_att='att1' second_att='foo'>8</level2>
      <level2 first_att='att2' second_att='bar'>8</level2>
    </level1>

可以通过以下方式访问属性“bar”:

import lxml.etree as etree
tree = etree.parse("test_file.xml")
print tree.xpath("//level1/level2[@first_att='att2']/@second_att")[0]

As a possible useful addition, this is how to get the value of an attribute in the case that the element has more than one, and it is the only difference with respect to another element.
E.g., given the following file.xml:

<?xml version ="1.0" encoding="UTF-8"?>
    <level1>
      <level2 first_att='att1' second_att='foo'>8</level2>
      <level2 first_att='att2' second_att='bar'>8</level2>
    </level1>

One can access the attribute 'bar' with:

import lxml.etree as etree
tree = etree.parse("test_file.xml")
print tree.xpath("//level1/level2[@first_att='att2']/@second_att")[0]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文