XPath:从 child 节点获取 parent 节点

XPath: Get parent node from child node

我需要为 child 节点获取 parent 节点 title 50

目前我只使用

//*[title="50"]

我怎样才能得到它的parent? 结果应该是 store 节点。


<?xml version="1.0" encoding="utf-8"?>
<d:data xmlns:d="defiant-namespace" d:mi="23">
    <store d:mi="22">
        <book price="12.99" d:price="Number" d:mi="4">
            <title d:constr="String" d:mi="1">Sword of Honour</title>
            <category d:constr="String" d:mi="2">fiction</category>
            <author d:constr="String" d:mi="3">Evelyn Waugh</author>
        </book>
        <book price="8.99" d:price="Number" d:mi="9">
            <title d:constr="String" d:mi="5">Moby Dick</title>
            <category d:constr="String" d:mi="6">fiction</category>
            <author d:constr="String" d:mi="7">Herman Melville</author>
            <isbn d:constr="String" d:mi="8">0-553-21311-3</isbn>
        </book>
        <book price="8.95" d:price="Number" d:mi="13">
            <title d:constr="String" d:mi="10">50</title>
            <category d:constr="String" d:mi="11">reference</category>
            <author d:constr="String" d:mi="12">Nigel Rees</author>
        </book>
        <book price="22.99" d:price="Number" d:mi="18">
            <title d:constr="String" d:mi="14">The Lord of the Rings</title>
            <category d:constr="String" d:mi="15">fiction</category>
            <author d:constr="String" d:mi="16">J. R. R. Tolkien</author>
            <isbn d:constr="String" d:mi="17">0-395-19395-8</isbn>
        </book>
        <bicycle price="19.95" d:price="Number" d:mi="21">
            <brand d:constr="String" d:mi="19">Cannondale</brand>
            <color d:constr="String" d:mi="20">red</color>
        </bicycle>
    </store>
</d:data>

使用 parent axes 和父节点的名称。

//*[title="50"]/parent::store

如果此 XPath 是 store,它只会 select 父节点。

但您也可以使用其中之一

//*[title="50"]/parent::*
//*[title="50"]/..

这些 xpath 将 select 任何父节点。因此,如果文档更改,您将始终 select 一个节点,即使它不是您期望的节点。

编辑

What happens in the given example where the parent is a bicycle but the parent of the parent is a store?

Does it ascent?

不,如果它是匹配 //*[title="50"] 的节点的父节点,它只会 select 存储商店。

If not, is there a method to ascent in such cases and return None if there is no such parent?

是的,您可以使用 ancestor

//*[title="50"]/ancestor::store

这将 select 匹配 //*[title="50"] 的节点的所有祖先是 ` 商店。例如

<data xmlns:d="defiant-namespace" d:mi="23">
    <store mi="1">
        <store mi="22">
            <book price="8.95" d:price="Number" d:mi="13">
                <title d:constr="String" d:mi="10">50</title>
                <category d:constr="String" d:mi="11">reference</category>
                <author d:constr="String" d:mi="12">Nigel Rees</author>
            </book>
        </store>
    </store>
</data>

作为替代方案,您可以使用 ancestor

//*[title="50"]/ancestor::store

它比parent更强大,因为它甚至可以得到祖父母或曾曾祖父母

你也可以在表达式末尾使用两个点。看这个例子:

//*[title="50"]/..

对旧的常见问题的新的、改进的回答...

How could I get its parent? Result should be the store node.

使用谓词而不是 parent::ancestor::

这里的大多数回答 select title 然后遍历到目标父或祖先 (store) 元素。一种更简单、直接的方法是首先直接 select 父元素或祖先元素,避免遍历到 parent::ancestor:: 轴的需要:

//*[book/title = "50"]

如果中间元素的名称不同:

//*[*/title = "50"]

或者,在名称和深度上:

//*[.//title = "50"]