gpt4 book ai didi

Python lxml : How to traverse back up a tree

转载 作者:太空宇宙 更新时间:2023-11-04 00:58:43 27 4
gpt4 key购买 nike

我有以下python代码

import lxml.etree

root = lxml.etree.parse("../../xml/test.xml")

path="./pages/page/paragraph[contains(text(),'ash')]"
para = root.xpath(path)

一旦我到达了para节点,我就不想再往前走了。现在我想回到根并查看所有 <paragraph>节点。有没有办法回到树上。

或者这样看。我想要 root 之间的子树和 para .我该怎么做?

供引用,这里是xml

<document>
<pages>
<page>
<paragraph>XBV</paragraph>
<paragraph>GFH</paragraph>
</page>
<page>
<paragraph>ash</paragraph>
<paragraph>lplp</paragraph>
</page>
</pages>
</document>

现在在这种情况下,我想要节点 XBV 和 GFH。这怎么可能?

最佳答案

.. 会让你在树上更上一层楼。

但是,我认为preceding是您正在寻找的东西:

The preceding axis indicates all the nodes that precede the context node in the document except any ancestor, attribute and namespace nodes.

./pages/page/paragraph[contains(text(),'ash')]//preceding::paragraph

示例代码:

import lxml.etree


data = """
<document>
<pages>

<page>
<paragraph>XBV</paragraph>

<paragraph>GFH</paragraph>
</page>

<page>
<paragraph>ash</paragraph>

<paragraph>lplp</paragraph>
</page>

</pages>
</document>
"""

tree = lxml.etree.fromstring(data)
print [item.text for item in tree.xpath("./pages/page/paragraph[contains(text(),'ash')]//preceding::paragraph")]

打印:

['XBV', 'GFH']

关于Python lxml : How to traverse back up a tree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33833358/

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