Project

Profile

Help

Support #4862

closed

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

Added by Ken Holman over 3 years ago. Updated over 3 years ago.

Status:
Closed
Priority:
Low
Assignee:
Category:
-
Sprint/Milestone:
-
Start date:
2020-12-22
Due date:
% Done:

0%

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

Description

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="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

<xsl:template match="/" name="start">
  <xsl:choose>
    <xsl:when test="exists(/)">Root exists&#xa;</xsl:when>
    <xsl:otherwise>Root does not exist&#xa;</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 $ 



Files

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

Updated by Michael Kay over 3 years 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.

Actions #2

Updated by Ken Holman over 3 years 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.

Actions #3

Updated by Michael Kay over 3 years 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