I am using xquery over BaseX XML Database. Say, I have the following documents stored in my database: doc1, doc2, doc3.
What Xquery do I use to check the existence of a given document. I tried the following:
declare variable $doc_name as xs:string external;
return boolean($doc_name)
I expected it to work as follows:
if doc_name = doc1 return true
if doc_name = nodoc return false
But, I see an exception:
java.io.IOException: Stopped at line 3, column 7:
[XPST0003] Unexpected end of query: ‘boolean(“doc1”)’.
I also tried return
-
return fn:exists($doc_name) and
-
return fn:doc-available(doc_uri)
These do not work either. I see the same end of query exception. What is the correct way of checking a document’s existence?
Thanks,
Sony
Use the standard XPath/XQuery function
doc-available().From the Spec:
fn:doc-available($uri as xs:string?) as xs:booleanSummary: The function returns true if and only if the function call
fn:doc($uri)would return a document node.If
$uriis an empty sequence, this function returnsfalse.If a call on
fn:doc($uri)would return a document node, this function returnstrue.If
$uriis not a valid URI according to the rules applied by the implementation offn:doc, an error is raised [err:FODC0005].Otherwise, this function returns
false.If this function returns
true, then callingfn:doc($uri)within the same ·execution scope· must return a document node. However, if non-stable processing has been selected for thefn:docfunction, this guarantee is lost.