Testing a non-existent root node (likely not a bug, but just checking)
I was expecting to pass the XPath address of a root node to exists(), but it appears I cannot. A transcript is below.
Is this a bug, or is there an obligation for Saxon to evaluate the XPath address before testing the node's existence?
No rush on a response ... I've worked around it easily ... but I thought I would report it in case this is an oversight.
Thanks for your thoughts!
Happy holidays to all of you!
. . . . . . Ken
~/t $ cat roottest.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:template match="/" name="start"> <xsl:choose> <xsl:when test="exists(/)">Root exists
</xsl:when> <xsl:otherwise>Root does not exist
</xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> ~/t $ saxon9he -xsl:roottest.xsl -s:roottest.xsl <?xml version="1.0" encoding="UTF-8"?>Root exists ~/t $ saxon9he -xsl:roottest.xsl -it:start Error at char 8 in xsl:when/@test on line 7 column 32 of roottest.xsl: XPDY0002: Finding root of tree: the context item is absent Finding root of tree: the context item is absent ~/t $
#1 Updated by Michael Kay 10 months ago
What you're doing seems intuitive, but it's wrong. If there's no context node, then "/" throws an error rather than returning an empty sequence. As far as I can see, the only way to test whether a context node exists is to use try/catch to catch the error when it doesn't. Surprising, but I think that's the situation.
#2 Updated by Ken Holman 10 months ago
You've convinced me because of the way you worded your answer. The address "/" isn't "the root of the input tree", it's "the root of the context node". So, yes I was wrong, when there is no context node, the address "/" is non-sensical. It is correct to evaluate the address first.
Sorry to take up the bandwidth. Thank you for your patience with me.
Please register to edit this issue