Using Marklogic 10's Query Console. I am trying to validate XML documents against two different schemas that I have loaded into the Schemas database. The XML documents I want to validate are in a database I created called "myDatabase". I want to be able to run validation against existing documents in the database and also run validation before inserting new documents into the database.
I've written two different queries (to validate existing documents and another to run before document creation), but the only way I can get them to work in Query Console is if I select the Documents database, select the App-Services server, and use xdmp:eval()
to get the documents from "myDatabase".
My question is, how can I run these queries against documents in "myDatabase" without using the xdmp:eval()
workaround?
I'm including the queries below:
xquery version "1.0-ml";
(: Validate existing documents :)
import schema namespace mods = "http://www.loc.gov/mods/v3" at "/mods-3-7.xsd";
import module namespace schematron = "http://marklogic.com/xdmp/schematron" at "/MarkLogic/schematron/schematron.xqy";
declare namespace svrl = "http://purl.oclc.org/dsdl/svrl";
let $query :=
"xquery version '1.0-ml';
let $doc := fn:doc('/test.xml')
return $doc"
let $docs := xdmp:eval($query, (),
<options xmlns="xdmp:eval">
<database>{xdmp:database("myDatabase")}</database>
</options>)
for $doc in $docs
return
(
try { concat(fn:document-uri(validate strict {$doc}), "
 MODS validation passed") }
catch ($e) { concat("MODS validation failed: ", $e/error:format-string/text()) },
schematron:validate($doc, schematron:get("/schematron.sch"))/svrl:schematron-output/svrl:failed-assert/svrl:text/concat(" Schematron error - ", text())
)
xquery version "1.0-ml";
(: Validate new documents before loading :)
import module namespace schematron = "http://marklogic.com/xdmp/schematron" at "/MarkLogic/schematron/schematron.xqy";
declare namespace svrl = "http://purl.oclc.org/dsdl/svrl";
let $node := xdmp:document-get("temp/test.xml")
let $query :=
"xquery version '1.0-ml';
import schema namespace mods = "http://www.loc.gov/mods/v3" at "/mods-3-7.xsd";
import module namespace schematron = "http://marklogic.com/xdmp/schematron" at "/MarkLogic/schematron/schematron.xqy";
declare variable $node as node()* external;
xdmp:document-insert('/test.xml', validate strict {$node} )"
return
(
try {
xdmp:eval($query, (xs:QName('node'), $node),
<options xmlns="xdmp:eval">
<database>{xdmp:database("myDatabase")}</database>
</options>)
}
catch ($e) { "Validation failed: ",
$e/error:format-string/text() },
schematron:validate($node, schematron:get("/schematron.sch"))/svrl:schematron-output/svrl:failed-assert/svrl:text/concat(" Schematron error - ", text())
)
Update: If I try to run the validation query or even just try to compile schematron in any database other than Documents I get the following error message:
[1.0-ml] XDMP-NODB: xdmp:eval("declare variable $validator-uri as xs:string external; decla...", (fn:QName("","validator-uri"), "/schematron.sch-validator.xsl", fn:QName("","validator-xslt"), ...), <options xmlns="xdmp:eval"><database>0</database></options>) -- No database with identifier 0