xml XPATH 选择根元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9497703/
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 15:14:18  来源:igfitidea点击:

XPATH selecting the root element

xmlxpath

提问by heymega

Is there a simple way of moving back to the root node within any given context. The XML document I'm working with is extremely large and would require using ../.. about a dozen times!!

是否有一种简单的方法可以在任何给定的上下文中移回根节点。我正在使用的 XML 文档非常大,需要使用 ../.. 大约十几次!!

Any help is greatly appreciated guys.

任何帮助都非常感谢伙计们。

采纳答案by keeney

I'm guessing you are in a predicate here? and want to return to look at data higher up the tree for your condition?

我猜你在这里是谓词?并想返回查看树更高层的数据以了解您的情况?

You should be able to start with a leading / and then work your way back down e.g

您应该能够从领先的 / 开始,然后再往下走,例如

/vehicles/cars/car[@id = /vehicles/featuredVehicle/@id]

回答by Frank Bollack

An XPath expression starting with /is always referring to the root element. Look herefor the syntax and some useful sample queries

以 开头的 XPath 表达式/始终引用根元素。看看这里的语法和一些有用的样本查询

If you want to select the root element itself, simply use /<element name>or /*

如果要选择根元素本身,只需使用/<element name>/*

回答by Dibbeke

You can use the ancestor axis. Say you have this document:

您可以使用祖先轴。假设你有这个文件:

<a><b><c></c></b></a>

Then a/b/c/ancestor::a brings the context to the c node and then back to the a node.

然后 a/b/c/ancestor::a 将上下文带到 c 节点,然后返回到 a 节点。

回答by deadlyvices

Yes. Use "/" as your XPath expression. This will select the root element for you. "//" selects the root and all its descendants.

是的。使用“/”作为 XPath 表达式。这将为您选择根元素。"//" 选择根及其所有后代。