2

In XPath 1.0 there's a function current() that comes in handy when I refer to a node somewhere else in the XML based on the node I'm in at the moment. My XML has a structure like this:

<root> <book> <chapter> <section> <para> <image id="1">1.png</image> </para> </section> </chapter> </book> <objects> <Object> <ID>1</ID> </Object> </objects> </root> 

When I process the para nodes in book, I sometimes use XPath expressions like this one to look up information in the objects nodes:

Object[ID=current()/descendant::image[1]/@id] 

When I switch my parser to XPath 2.0, this expression gives an error (function current is not available).

I've done some searching, but haven't been able to find a straight replacement for this function. Is there one?

2 Answers 2

3
+100

The current() function is defined in XSLT, not in XPath. It's available in XPath expressions within XSLT 1.0, 2.0, and 3.0 stylesheets, but it's not available in free-standing XPath (in any version, except perhaps as a non-conformant extension).

In XPath 2.0 there's a workaround using variables:

Object[ID=current()/descendant::image[1]/@id] 

becomes

for $x in descendant::image[1]/@id return Object[ID=$x] 
Sign up to request clarification or add additional context in comments.

3 Comments

I'm using the XPath/XQuery builder in Oxygen 15.0. When I set that to use XPath 1.0, it accepts the query, when I set it to use XPath 2.0 the error appears. I'll do some more tests.
That's then an Oxygen issue.
@Hobbes - I'm also able to reproduce that behavior in oXygen 16.1. Maybe try searching/posting the oXygen forum.
3

The current() function is only available in XSLT (all versions). It's not available in XPath (any version).

See the current() function description in the "Additional Functions" section of the spec.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.