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

I am using xquery for a BaseX XML database. Let's say I have the following documents stored in my database: doc1, doc2, doc3.

Which Xquery I use to verify the existence of this document. I tried the following:

declare the 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 row 3, column 7: [XPST0003] Unexpected end of request: 'boolean ("doc1")'.

I also tried to return

  • return fn: exists ($ doc_name) and

  • return fn: doc-available (doc_uri)

They do not work either. I see the same end to the query exception. What is the correct way to verify the existence of a document?

Thanks Sony

+3
source share
1 answer

Use the standard XPath / XQuery functiondoc-available() .

From Spec:

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

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

If $uriis an empty sequence, this function returns false.

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

If $uriit is not a valid URI in accordance with the rules applicable during implementation fn:doc, an error occurs [err: FODC0005].

Otherwise, this function returns false.

true, fn:doc($uri) node. fn:doc , .

+5

Source: https://habr.com/ru/post/1789552/


All Articles