Support #4862

Testing a non-existent root node (likely not a bug, but just checking)

Added by Ken Holman 10 months ago. Updated 10 months ago.

Start date:
Due date:
% Done:


Estimated time:
Legacy ID:
Applies to branch:
Fix Committed on Branch:
Fixed in Maintenance Release:


Hi, all!

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=""

<xsl:template match="/" name="start">
    <xsl:when test="exists(/)">Root exists&#xa;</xsl:when>
    <xsl:otherwise>Root does not exist&#xa;</xsl:otherwise>

~/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 $ 

roottest.xsl (345 Bytes) roottest.xsl Ken Holman, 2020-12-22 21:13


#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.

#3 Updated by Michael Kay 10 months ago

  • Tracker changed from Bug to Support
  • Status changed from New to Closed
  • Assignee set to Michael Kay

Please register to edit this issue

Also available in: Atom PDF