gpt4 book ai didi

python - 带有 lxml 子路径的 XPath 谓词?

转载 作者:太空狗 更新时间:2023-10-29 21:15:44 25 4
gpt4 key购买 nike

我正在尝试理解发送给我的用于 ACORD XML 表单(保险中的通用格式)的 XPath。他们发给我的 XPath 是(为简洁起见被截断了):

./PersApplicationInfo/InsuredOrPrincipal[InsuredOrPrincipalInfo/InsuredOrPrincipalRoleCd="AN"]/GeneralPartyInfo

我遇到麻烦的地方是 Python 的 lxml library告诉我 [InsuredOrPrincipalInfo/InsuredOrPrincipalRoleCd="AN"] 是一个无效谓词。我无法在 XPath spec on predicates 中找到任何地方它标识了此语法,以便我可以修改此谓词以使其工作。

是否有关于此谓词究竟选择了什么的文档?此外,这甚至是一个有效的谓词,还是在某处被破坏了?

可能相关:

我相信与我合作的公司是一家 MS 商店,所以这个 XPath 可能在 C# 或该堆栈中的其他一些语言中有效?我不完全确定。

更新:

根据评论要求,这里有一些额外的信息。

XML 示例:

<ACORD>
<InsuranceSvcRq>
<HomePolicyQuoteInqRq>
<PersPolicy>
<PersApplicationInfo>
<InsuredOrPrincipal>
<InsuredOrPrincipalInfo>
<InsuredOrPrincipalRoleCd>AN</InsuredOrPrincipalRoleCd>
</InsuredOrPrincipalInfo>
<GeneralPartyInfo>
<Addr>
<Addr1></Addr1>
</Addr>
</GeneralPartyInfo>
</InsuredOrPrincipal>
</PersApplicationInfo>
</PersPolicy>
</HomePolicyQuoteInqRq>
</InsuranceSvcRq>
</ACORD>

代码示例(使用完整的 XPath 而不是片段):

>>> from lxml import etree
>>> tree = etree.fromstring(raw)
>>> tree.find('./InsuranceSvcRq/HomePolicyQuoteInqRq/PersPolicy/PersApplicationInfo/InsuredOrPrincipal[InsuredOrPrincipalInfo/InsuredOrPrincipalRoleCd="AN"]/GeneralPartyInfo/Addr/Addr1')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "lxml.etree.pyx", line 1409, in lxml.etree._Element.find (src/lxml/lxml.etree.c:39972)
File "/Library/Python/2.5/site-packages/lxml-2.3-py2.5-macosx-10.3-i386.egg/lxml/_elementpath.py", line 271, in find
it = iterfind(elem, path, namespaces)
File "/Library/Python/2.5/site-packages/lxml-2.3-py2.5-macosx-10.3-i386.egg/lxml/_elementpath.py", line 261, in iterfind
selector = _build_path_iterator(path, namespaces)
File "/Library/Python/2.5/site-packages/lxml-2.3-py2.5-macosx-10.3-i386.egg/lxml/_elementpath.py", line 245, in _build_path_iterator
selector.append(ops[token[0]](_next, token))
File "/Library/Python/2.5/site-packages/lxml-2.3-py2.5-macosx-10.3-i386.egg/lxml/_elementpath.py", line 207, in prepare_predicate
raise SyntaxError("invalid predicate")
SyntaxError: invalid predicate

最佳答案

tree.find 更改为 tree.xpathfindfindall 出现在 lxml 中以提供与 ElementTree 的其他实现的兼容性。 These methods do not implement the entire XPath language .要使用包含更多高级功能的 XPath 表达式,请使用 xpath 方法、XPath 类或 XPathEvaluator

例如:

import io
import lxml.etree as ET

content='''\
<ACORD>
<InsuranceSvcRq>
<HomePolicyQuoteInqRq>
<PersPolicy>
<PersApplicationInfo>
<InsuredOrPrincipal>
<InsuredOrPrincipalInfo>
<InsuredOrPrincipalRoleCd>AN</InsuredOrPrincipalRoleCd>
</InsuredOrPrincipalInfo>
<GeneralPartyInfo>
<Addr>
<Addr1></Addr1>
</Addr>
</GeneralPartyInfo>
</InsuredOrPrincipal>
</PersApplicationInfo>
</PersPolicy>
</HomePolicyQuoteInqRq>
</InsuranceSvcRq>
</ACORD>
'''
tree=ET.parse(io.BytesIO(content))
path='//PersApplicationInfo/InsuredOrPrincipal[InsuredOrPrincipalInfo/InsuredOrPrincipalRoleCd="AN"]/GeneralPartyInfo'
result=tree.xpath(path)
print(result)

产量

[<Element GeneralPartyInfo at b75a8194>]

tree.find 产出

SyntaxError: invalid node predicate

关于python - 带有 lxml 子路径的 XPath 谓词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6218126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com