在 python 中评估 Xpath2.0 [英] Evaluate Xpath2.0 in python

查看:38
本文介绍了在 python 中评估 Xpath2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an XPath expression as shown below.

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"表达式有效.

I want to display which 'if ' expression worked.

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

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

'If' expression can have nested if, for loops and xpath2.0 functions.

我找不到任何适用于 python 的 xpath2.0 库.所以我尝试将此 Js library 转换为 python 仍然可以将 xpath2.0 表达式拆分为词法分析器,但无法将其完全转换为 python.

I can't find any xpath2.0 library for python. So I tried to convert this Js library to python still I can able to split xpath2.0 expression to lexers but can't convert it fully to python.

建议我一些适用于 python 的 Xpath2.0 库,如果有的话.还有如何解释 XPath 表达式并显示表达式的哪一部分有效?

Suggest me some Xpath2.0 library for python, if available. Also how to interpret XPath expression and display which part of the expression worked?

推荐答案

如您所知,lxml,Python 的 XML/XPath 支持的基石,只有特性

As you already know, lxml, the cornerstone of Python's XML/XPath support, features only

XPath 1.0、XSLT 1.0 和 EXSLT 扩展(通过 libxml2 和 libxslt)

XPath 1.0, XSLT 1.0 and the EXSLT extensions through libxml2 and libxslt

我们还有一些选择.

我最近研究了这个话题(特别是 Python 的 XQuery 支持).
请参阅 W3C 以获取 XML 查询实现的参考列表.

I researched this topic recently (specifically, Python's XQuery support).
See the W3C for a reference list of XML Query Implementations.

  1. 带有 XPath 2+ 和 EXSLT 扩展的 Python 模块(例如 EXSLT 用于正则表达式 matching)
    PiPy 上的一些模块 部分提供了 XPath 2.0+ 功能.

  1. Python Modules with XPath 2+ and EXSLT Extensions (e.g. an EXSLT for regex matching)
    There are some modules on PiPy that partially offer XPath 2.0+ functionality.

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

There are some OSS XML/NoSQL-DBMS that implement XPath/XQuery 2.0 functions, e.g.

  • Zorba,XQuery 1.0/2.0 的开源可移植嵌入式 C++ 实现,具有 Python 绑定(这个问题有一些提示),
  • 以及 Sedna 和一些商业 DBMS.根据您的项目,这可能是一个不错的选择.
  • Zorba, an open source portable embeddable C++ implementation of XQuery 1.0/2.0 which has Python bindings (this question has some pointers),
  • as well as Sedna and some commercial DBMS. Depending on your project, this could be a good choice.

我相信 Saxon/C(来自 Michael Kay) 与 Cython 是最有前途的道路.
在使用 Boost.Pythonpysaxon.
更新:适用于 Python 3 的 Saxon/C 扩展同时发布.

I believe Saxon/C (by Michael Kay) with Cython is the most promising road.
It was tried before using Boost.Python and at pysaxon.
Update: A Saxon/C extension for Python 3 has been published meanwhile.

您可以使用子进程调用 CLI XML 处理器(如此处的建议),例如subprocess.call(["saxon", "-o:output.xml", "-s:file.xml", "file.xslt"])

You could use a subprocess to call a CLI XML processor (as suggested here), e.g. subprocess.call(["saxon", "-o:output.xml", "-s:file.xml", "file.xslt"])

另一种选择是将 XSLT/XPath/XQuery 与撒克逊和/或其他Java XML 类Jython.

Another option is using XSLT/XPath/XQuery with saxon and/or other Java XML classes in Jython.

最后,您可以设置一个 Web 服务,用 Java、.NET 等语言为您完成繁重的工作,并提供适当的 XPath 3+ 支持(也由 Kay 此处).

Finally, you could set-up a web service that does the hard work for you in a language like Java, .NET, etc, that comes with proper XPath 3+ support (also suggested by Kay here).

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

Still somewhat disappointing, especially for a big language like Python.

这篇关于在 python 中评估 Xpath2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆