Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can I use current() function in XPath, outside of XSLT?

Tags:

xml

xpath

This is my XML document:

<root>
  <bad>
    <id>13</id>
    <id>27</id>
  </bad>
  <books>
    <book id='5'/>
    <book id='7'/>
    <book id='13'/>
  </books>
</root>

Now I'm trying to select all books that are not "bad":

/root/books/book[not(/root/bad/id[.=@current()/@id])]

This doesn't work. I'm getting all books, while book no.13 should be excluded. It's not XSLT. It's just an XPath request (I'm with Java). What's wrong?

like image 953
Barbara Krein Avatar asked Apr 09 '15 23:04

Barbara Krein


People also ask

What is current node in XPath?

Current node is the node that the XPath processor is looking at when it begins evaluation of a query. In other words, the current node is the first context node that the XPath processor uses when it starts to execute the query. During evaluation of a query, the current node does not change.

Which node types are valid in XPath?

In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document nodes. XML documents are treated as trees of nodes. The topmost element of the tree is called the root element.


1 Answers

The current() function is only supported by XSLT. But there's no need to use current() here. You can get the result you want with the following expression:

/root/books/book[not(@id=/root/bad/id)]
like image 112
nwellnhof Avatar answered Oct 16 '22 14:10

nwellnhof