Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find if a document exists in the database using xquery

Tags:

xquery

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

like image 800
sony Avatar asked Oct 11 '25 11:10

sony


1 Answers

Use the standard XPath/XQuery function doc-available() .

From the Spec:

fn:doc-available($uri as xs:string?) as xs:boolean

Summary: The function returns true if and only if the function call fn:doc($uri) would return a document node.

If $uri is an empty sequence, this function returns false.

If a call on fn:doc($uri) would return a document node, this function returns true.

If $uri is not a valid URI according to the rules applied by the implementation of fn:doc, an error is raised [err:FODC0005].

Otherwise, this function returns false.

If this function returns true, then calling fn:doc($uri) within the same ·execution scope· must return a document node. However, if non-stable processing has been selected for the fn:doc function, this guarantee is lost.

like image 110
Dimitre Novatchev Avatar answered Oct 14 '25 03:10

Dimitre Novatchev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!