在python中评估Xpath2.0

2024-02-26

我有一个 XPath 表达式,如下所示。

if(replace(//p[1]/text(),'H','h') = 'hello') then //p[1]/text() else if(//p[1]/text() = 'world') then //p[2]/text() else 'notFound'

我想显示哪个“if”表达式有效。

e.g //p[1]/text() if first 'if' expression worked.

“If”表达式可以嵌套 if、for 循环和 xpath2.0 函数。

我找不到任何适用于 python 的 xpath2.0 库。所以我尝试转换这个Jslibrary https://github.com/ilinsky/jquery-xpath到 python 仍然可以将 xpath2.0 表达式拆分为词法分析器,但无法将其完全转换为 python。

给我推荐一些适用于 python 的 Xpath2.0 库(如果有的话)。 另外,如何解释 XPath 表达式并显示表达式的哪一部分有效?


就像你已经知道的那样,lxml https://lxml.de/xpathxslt.html,Python XML/XPath 支持的基石,仅功能

XPath 1.0、XSLT 1.0 以及通过 libxml2 和 libxslt 的 EXSLT 扩展

我们还有一些选择。

我最近研究了这个主题(特别是 Python 的 XQuery 支持)。
请参阅 W3C 的参考列表XML 查询实现 http://w3.org/XML/Query/#implementations.

  1. 具有 XPath 2+ 和 EXSLT 扩展的 Python 模块(例如EXSLT https://stackoverflow.com/questions/34047567/xpathevalerror-unregistered-function-for-matches-in-lxml对于正则表达式match http://exslt.org/regexp/functions/match/ing)
    PyPi 上的一些模块 https://pypi.org/search/?q=%22xpath%202.0%22部分提供 XPath 2.0+ 功能。

  2. 有一些 OSS XML/NoSQL-DBMS 实现了 XPath/XQuery 2.0 功能,例如

    • Zorba http://www.zorba-xquery.com/,XQuery 1.0/2.0 的开源可移植可嵌入 C++ 实现,具有 Python 绑定(此question https://stackoverflow.com/questions/2133648/xquery-library-under-python有一些指示),
    • Sedna https://github.com/bitkeeper/zif.sedna以及一些商业 DBMS。根据您的项目,这可能是一个不错的选择。
  3. 我相信Saxon/C http://www.saxonica.com/saxon-c/index.xml (by 迈克尔·凯 https://stackoverflow.com/users/415448/michael-kay)用Cython是最有前途的一条路。
    使用前曾尝试过Boost.Python https://www.boost.org/doc/libs/1_66_0/libs/python/doc/html/index.html and at pysaxon https://github.com/ajelenak/pysaxon.
    Update: A Python 3 的 Saxon/C 扩展 http://saxonica.com/saxon-c/index.xml已同时发布。

  4. 您可以使用子进程来调用 CLI XML 处理器(如建议的here https://stackoverflow.com/a/40167794/8291949), e.g. subprocess.call(["saxon", "-o:output.xml", "-s:file.xml", "file.xslt"])

  5. 另一种选择是使用XSLT https://gist.github.com/lawlesst/3370069/XPath/XQuery 与 saxon 和/或其他Java XML 类 https://wiki.python.org/jython/XmlRelatedExamples in Jython http://www.jython.org/.

  6. 最后,您可以设置一个 Web 服务,用 Java、.NET 等语言为您完成艰苦的工作,并提供适当的 XPath 3+ 支持(Kay 也建议)here https://stackoverflow.com/a/29444576/8291949).

还是有些令人失望,尤其是对于像 Python 这样的大语言。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在python中评估Xpath2.0 的相关文章

随机推荐