xml XPath:从子节点获取父节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28237694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:06:42  来源:igfitidea点击:

XPath: Get parent node from child node

xmlxpath

提问by GibboK

I need get the parent node for child node title 50

我需要获取子节点的父节点 title 50

At the moment I am using only

目前我只使用

//*[title="50"]

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

我怎么能得到它的父母?结果应该是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>

回答by René Link

Use the parentaxeswith the parent node's name.

使用带有父节点名称的parent

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

This XPath will only select the parent node if it is a store.

这个 XPath 只会选择父节点,如果它是一个store.

But you can also use one of these

但您也可以使用其中之一

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

These xpaths will select any parent node. So if the document changes you will always select a node, even if it is not the node you expect.

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

EDIT

编辑

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

Does it ascent?

在给定的示例中,父节点是自行车,而父节点的父节点是商店,会发生什么?

它上升吗?

No, it only selects the store if it is a parent of the node that matches //*[title="50"].

不,它仅在它是匹配节点的父节点时才选择商店//*[title="50"]

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

如果没有,是否有一种方法可以在这种情况下上升并在没有这样的父母时返回 None ?

Yes, you can use ancestoraxes

是的,您可以使用ancestor

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

This will select all ancestors of the node matching //*[title="50"]that are ` stores. E.g.

这将选择匹配节点的所有祖先,//*[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>

XPath selection result

XPath 选择结果

回答by Aminah Nuraini

Just as an alternative, you can use ancestor.

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

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

It's more powerful than parentsince it can get even the grandparent or great great grandparent

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

回答by phduarte

You can use the two dots at the end of expression, too. See this example:

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

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

回答by kjhughes

New, improved answer to an old, frequently asked question...

对一个旧的、常见的问题的新的、改进的答案......

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

我怎么能得到它的父母?结果应该是store节点。

Use a predicate rather than the parent::or ancestor::axis

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

Most answers here select the titleand then traverse up to the targeted parent or ancestor (store) element. A simpler, direct approach is to select parent or ancestor element directly in the first place, obviating the need to traverse to a parent::or ancestor::axes:

此处的大多数答案选择title,然后遍历到目标父或祖先 ( store) 元素。一种更简单、直接的方法是首先直接选择父元素或祖先元素,从而无需遍历 aparent::ancestor::轴:

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

Should the intervening elements vary in name:

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

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

Or, in name and depth:

或者,在名称和深度上:

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

回答by Shivam Bharadwaj

This works in my case. I hope you can extract meaning out of it.

这在我的情况下有效。我希望你能从中提取意义。

//div[text()='building1' and @class='wrap']/ancestor::tr/td/div/div[@class='x-grid-row-checker']