xml 选择一系列节点的 XPath 是什么?

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

What is the XPath to select a range of nodes?

xmlxpath

提问by Shawn

I have an XML file that's structured like this:

我有一个结构如下的 XML 文件:

 <foo>
     <bar></bar>
     <bar></bar>
     ...
</foo>

I don't know how to grab a range of nodes. Could someone give me an example of an XPath expression that grabs bar nodes 100-200?

我不知道如何抓取一系列节点。有人能给我一个 XPath 表达式的例子,它可以抓取 100-200 个条形节点吗?

回答by Dimitre Novatchev

Use:

使用

/*/bar[position() >= 100 and not(position() > 200)]

Do note:

请注意

  1. Exactly the barelements at position 100 to 200 (inclusive) are selected.

  2. The evaluation of this XPath expressions can be many times faster than an expression using the //abbreviation, because the latter causes a complete scan of the tree whose root is the context node. Always try to avoid using the //abbreviationin cases when this is possible.

  1. 正好选择bar位置 100 到 200(含)的元素。

  2. 对这个 XPath 表达式的计算可能比使用//缩写的表达式快很多倍,因为后者会导致对以上下文节点为根的树的完整扫描。在可能的情况下,始终尽量避免使用//缩写

回答by kennytm

//foo/bar[100 <= position() and position() < 200]

回答by CiaPan

Isn't fn:subsequencethe best way?

不是fn:subsequence最好的办法吗?

subsequence( /foo/bar, 100, 101 )

returns all items from position 100 through 200, that is 101 items (or less if the source sequence is shorter).

返回从位置 100 到 200 的所有项目,即 101 个项目(如果源序列较短,则更少)。