简体   繁体   English

XPath中有两组方括号(如2D数组)是什么意思?

[英]What do two sets of square brackets (like a 2D array) mean in XPath?

In XPath, what does it mean when you write a query that includes two consecutive values enclosed in [] , like a Java array [][] 在XPath中,当你编写一个包含[]中包含的两个连续值的查询时,它是什么意思,就像Java数组[][]

For example: 例如:

//bookstore/book[3][1]

When I do this, I get the 3rd book as the result (the same if I would have just said //bookstore/book[3] 当我这样做时,我得到第3本书作为结果(如果我刚才说//bookstore/book[3]

I've been trying to play with this and determine what exactly is happening, for example, I tried //bookstore/book[3][2] and //bookstore/book[3][0] but got no results. 我一直试图用这个来确定究竟发生了什么,例如,我试过//bookstore/book[3][2]//bookstore/book[3][0]但没有得到任何结果。 It seems to only work if the second value is 1 but I have no clue why. 它似乎只有在第二个值为1时才有效,但我不知道为什么。

Here is the XML I'm playing with: 这是我正在玩的XML:

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>

<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J. K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

<book category="WEB">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
</book>

<book category="WEB">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>2003</year>
  <price>39.95</price>
</book>

<book category="OPENSOURCE">
  <title lang="en">Open Source</title>
  <year>2003</year>
  <price>39.95</price>
</book>

</bookstore>

The predicates operate in a context. 谓词在上下文中运行。 When you use //bookstore/book[3] (which is equivalent to //bookstore/book[position()=3] ) you are restricting the node-set to the matching node in the third position. 当您使用//bookstore/book[3] (相当于//bookstore/book[position()=3] )时,您将节点集限制在第三个位置的匹配节点。 In that context if you add an extra predicate like [1] you are simply selecting the one and only node available. 上下文中,如果添加一个额外的谓词,如[1] ,则只需选择唯一可用的节点。

This is useful if your first predicate results in a node-set which you wish to add more restrictions with another predicate. 如果您的第一个谓词导致您希望使用另一个谓词添加更多限制的节点集,这将非常有用。 For example, if your first predicate had restricted the nodes by selecting the year 例如,如果您的第一个谓词通过选择年份来限制节点

//bookstore/book[year=2005]

you would have two nodes, and you could select the first one of those 2 with a second predicate, selecting the position: 你将有两个节点,你可以选择第二个谓词中的第一个节点,选择位置:

//bookstore/book[year=2005][1]

You can add any number of [1]s after you have only one node, since it the result will always a node-set containing one node, and you are selecting the one in the first position: 只有一个节点后,可以添加任意数量的[1],因为结果将始终是包含一个节点的节点集,并且您在第一个位置选择一个节点:

//bookstore/book[3][1][1][1][1][1][1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM