简体   繁体   English

(//.) XPath 中的表达式

[英](//.) Expression in XPath

I have this little problem in getting an XPATH expression result !我在获取 XPATH 表达式结果时遇到了这个小问题!

Let's say i have this little XML file :假设我有这个小 XML 文件:

<bookstore>
 <book>
  <title lang="en">Learning Java</title>
  <price>29.99</price>
 </book>

 <book>
  <title lang="en">Learning Xpath</title>
  <price>39.95</price>
 </book>
</bookstore>

What will be the result of :结果会是什么:

//book[//.='Lear']

Thank you谢谢

What will be the result of :结果会是什么:

//book[//.='Lear']

You can always dump XML sample and xpath expression in an xpath tester and see the result by yourself (fe using http://www.freeformatter.com/xpath-tester.html , or whatever you like).您始终可以在 xpath 测试器中转储 XML 示例和 xpath 表达式,然后自己查看结果(使用http://www.freeformatter.com/xpath-tester.html或您喜欢的任何内容)。 For the above xpath and XML sample, the result will be nothing.对于上面的 xpath 和 XML 示例,结果将是空的。 Given that particular XML input, the above xpath expression is the same as //book[false()] .鉴于特定的 XML 输入,上面的 xpath 表达式与//book[false()] The predicate (content of [] ) evaluate to false because there is no element containing exact string "Lear" .谓词( []内容)评估为false因为没有包含精确字符串"Lear"元素。

"But can you tell me what's useful about the dot after the double slash symbol ?" “但是你能告诉我双斜线符号后面的点有什么用吗?”

To answer that comment, see the following break-down :要回答该评论,请参阅以下细分:

  • // : Abbreviated syntax for descendant-or-self axis. // : descendant-or-self轴的缩写语法。

  • . : Reference current context node. : 引用当前上下文节点。

  • //. : You can read this as find any node anywhere in the XML document . :您可以将其理解为在 XML 文档中的任何位置查找任何节点 That can also be expressed as //self::node() .这也可以表示为//self::node() Note that // starts searching from the root element, it doesn't care about current book element being the context.注意//从根元素开始搜索,它不关心当前的book元素是上下文。

如果您要查找所有标题为“Lear”的书籍,请尝试以下操作:

//book[contains(title,'Lear')]

//book selects all book elements that are descendants of the current context (in your case that would be the root element). //book选择作为当前上下文的后代的所有book元素(在您的情况下,这将是根元素)。

[] indicates a condition to apply to the selection, so in essence you will be filtering book elements. []表示应用于选择的条件,因此本质上您将过滤book元素。

Inside the square brackets [] the current context becomes book so // means any descendant of book.在方括号[]当前上下文变为book因此//表示 book 的任何后代。 . simply denotes the element itself which is used here to apply the = operator to.仅表示此处用于应用=运算符的元素本身。 So // means all descendants, //.所以//表示所有后代, //. means "what follows applies to the element which is any descendant`.表示“以下内容适用于任何后代的元素。

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

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