简体   繁体   中英

(//.) Expression in XPath

I have this little problem in getting an XPATH expression result !

Let's say i have this little XML file :

<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). For the above xpath and XML sample, the result will be nothing. Given that particular XML input, the above xpath expression is the same as //book[false()] . The predicate (content of [] ) evaluate to false because there is no element containing exact string "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.

  • . : Reference current context node.

  • //. : You can read this as find any node anywhere in the XML document . That can also be expressed as //self::node() . Note that // starts searching from the root element, it doesn't care about current book element being the context.

如果您要查找所有标题为“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).

[] indicates a condition to apply to the selection, so in essence you will be filtering book elements.

Inside the square brackets [] the current context becomes book so // means any descendant of 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`.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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