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?
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.
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.
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)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With