gpt4 book ai didi

python - 相同的 xpath 在 Centos、Ubuntu 上返回不同的值

转载 作者:太空狗 更新时间:2023-10-29 12:26:04 26 4
gpt4 key购买 nike

我们正在尝试将我们的应用程序从 Centos 环境移植到其他 Linux,尤其是 Ubuntu。该应用程序是基于 python 的,因此应该没有问题,但是我们在解析 XPATH 时注意到一个奇怪的行为。

示例文件:

<root>
<outer>
<inner>
<el/>
</inner>
<inner>
<el/>
</inner>
</outer>
</root>

代码:

from lxml import etree
r = etree.parse('foo.xml')
print 'One: ', r.xpath('.//el[2]')
print 'Two: ', r.xpath('(.//el)[2]')

在 Centos(python 2.7.5)上:

One:  [<Element el at 0x25fdcf8>]
New version: [<Element el at 0x25fdcf8>]

在 RedHat Enterprise 6.6(python 2.7.11)上

One:  []
Two: [<Element el at 0x7fa27198cd40>]

在 Debian 和 Ubuntu(python 2.7.9 和 2.7.12)上:

One:  []
Two: [<Element el at 0x7f94ed6cf200>]

这是试图访问第二个元素 el相同代码。

Debian、Ubuntu 和 Cents 环境都有 lxml(从 pip 安装)版本 3.6.4。安装 lxml 时,它会针对 libxml2 进行编译,即

在 debian 上:

    $ dpkg -l | grep libxml2-dev
ii libxml2-dev:amd64 2.9.1+dfsg1-5+deb8u2 amd64

在 centos 上:

$ rpm -q libxml2-devel
libxml2-devel-2.9.1-6.el7_2.3.x86_64

RedHat 运行 etree 3.6.0:

$ rpm -q libxml2-devel
libxml2-devel-2.7.6-21.el6.x86_64

这是怎么回事?

最佳答案

CentOS 上的结果不正确。

libxml2 2.9.0 引入了回归,参见 libxml 2.9.0 XPath evaluation issue .它在 2.9.2 中已修复,但在 2.9.1 中未修复。

Debian 已经在 2.9.1+dfsg1-3 版本中集成了补丁:

libxml2 (2.9.1+dfsg1-3) unstable; urgency=low

  • debian/patches/0007-Fix-XPath-optimization-with-predicates.patch:
    • Upstream patch to fix XPath evaluation issue. (Closes: #713146)

关于python - 相同的 xpath 在 Centos、Ubuntu 上返回不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39899632/

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