简体   繁体   English

XPath SyntaxError: 无效谓词

[英]XPath SyntaxError: invalid predicate

I have a XML file like this:我有一个像这样的 XML 文件:

$ cat sample.xml
<Requests>
        <Request>
                <ID>123</ID>
                <Items>
                        <Item>a item</Item>
                        <Item>b item</Item>
                        <Item>c item</Item>
                </Items>
        </Request>
        <Request>
                <ID>456</ID>
                <Items>
                        <Item>d item</Item>
                        <Item>e item</Item>
                </Items>
        </Request>
</Requests>

I simply want to extract the XML of Request elements which has certain value for their grandchild element Item .我只是想提取对其孙元素Item具有一定价值的Request元素的 XML 。 Here is code:这是代码:

bash-4.2$ cat xsearch.py
import sys
import xml.etree.ElementTree as ET


if __name__ == '__main__':
        tree = ET.parse(sys.argv[1])
        root = tree.getroot()
        for request in root.findall(".//Item[.='c item']/../.."):
        #for request in root.findall(".//Request[Items/Item = 'c item']"):
                print(request)

I got "invalid predicate" error:我收到“无效谓词”错误:

bash-4.2$ python3 xsearch.py sample.xml
Traceback (most recent call last):
  File "/usr/lib64/python3.6/xml/etree/ElementPath.py", line 263, in iterfind
    selector = _cache[cache_key]
KeyError: (".//Item[.='c item']/../..", None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "xsearch.py", line 8, in <module>
    for request in root.findall(".//Item[.='c item']/../.."):
  File "/usr/lib64/python3.6/xml/etree/ElementPath.py", line 304, in findall
    return list(iterfind(elem, path, namespaces))
  File "/usr/lib64/python3.6/xml/etree/ElementPath.py", line 277, in iterfind
    selector.append(ops[token[0]](next, token))
  File "/usr/lib64/python3.6/xml/etree/ElementPath.py", line 233, in     prepare_predicate
    raise SyntaxError("invalid predicate")
SyntaxError: invalid predicate

 

Could any one point out where I got it wrong?有人能指出我哪里弄错了吗?

In general, an XPath invalid predicate error means something is syntactically wrong with one of the XPath's predicates, the code between the [ and ] .一般而言,XPath无效谓词错误意味着 XPath 的谓词之一(即[]之间的代码)在语法上存在错误。

Specifically in your case, there are two issues:特别是在您的情况下,有两个问题:

  1. The SyntaxError("invalid predicate") is because there's an extra ) in the predicate: SyntaxError("invalid predicate")是因为谓词中有一个额外的)

     for request in root.findall(".//Item[.='c item')]/../.."): ^

    Note also that you can hoist the predicate to avoid navigating down and then back up ( ../.. ):另请注意,您可以提升谓词以避免向下导航然后备份( ../.. ):

    Instead of代替

     .//Item[.='c item']/../..

    consider考虑

     .//Request[Items/Item = 'c item']

    to select the Request element with the targeted Item .到 select Request元素与目标Item

  2. The XPath library you're using, ElementTree, is not a full implementation of the XPath standard .您使用的 XPath 库ElementTree 不是 XPath 标准的完整实现 You can waste a lot of time trying to identify what ElementTree does support ( ".//Items[Item='c item']/.." happens to work here) and does not support, but it'd be better to just use a more compliant library such as lxml .您可能会浪费大量时间来确定 ElementTree 支持什么( ".//Items[Item='c item']/.."恰好在这里工作)并且不支持,但最好只使用更合规的库,例如lxml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM