Hi Eliot,
The XQuery Specification allows implementations to simplify an expression if it always yields the same result in the case of success. As db:node-id never returns an empty sequence, BaseX rewrites exists(db:node-id()) to true().
You can use the following alternative:
declare function util:isDatabaseNode($node) { try { boolean(db:name($node)) } catch db:node { false() } };
It would be cleaner if we added a dedicated function for that in a future version of BaseX.
Cheers, Christian
On Mon, Jul 3, 2023 at 9:55 PM Eliot Kimber eliot.kimber@servicenow.com wrote:
I need to be able to determine if an arbitrary node is or is not a database node.
I implemented this function:
declare function util:isDatabaseNode($node as node()) as xs:boolean {
let $isDbNode as xs:boolean :=
try { let $nodeId as xs:integer? := db:node-id($node) (: let $msg := prof:dump(``[[DEBUG] relpath:isDatabaseNode(): nodeId: "`{$nodeId}`"]``) :) return exists($nodeId) } catch * { (: let $msg := prof:dump(``[[DEBUG] relpath:isDatabaseNode(): Got exception `{$err:code}` - `{$err:description}`]``) :) (: return :) false() }
let $msg := prof:dump(``[[DEBUG] relpath:isDatabaseNode(): Returning `{$isDbNode}``]``)
return $isDbNode
};
With the prof:dump() calls commented out as shown here, it always returns true, even when it returned false.
If I comment in the prof:dumps then I get the expected correct false result.
This must be the result some optimization at compile time but I haven’t been able to find a construction of this function that works without the debug messages.
Is there a better way to determine if a node is a database node?
This is with 10.6.
Cheers,
E.
Eliot Kimber
Sr Staff Content Engineer
O: 512 554 9368
M: 512 554 9368
servicenow.com
LinkedIn | Twitter | YouTube | Facebook