简体   繁体   中英

How can I validate XML documents in a specific database against a schema in the Schemas database in Marklogic's query console?

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}), "&#xa;  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 = &quot;http://www.loc.gov/mods/v3&quot; at &quot;/mods-3-7.xsd&quot;;
  import module namespace schematron = &quot;http://marklogic.com/xdmp/schematron&quot; at &quot;/MarkLogic/schematron/schematron.xqy&quot;; 
  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;&#10;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

Short answer, check if Schemas is selected as schemas database for your myDatabase documents database.

The app server that you evaluate against dictates which documents and modules database should be used. The documents database in turn dictates which schemas and triggers database go with it. If you create an empty database in Admin UI, it sets schemas and triggers to (none) by default. Same might apply to other methods to create a database.

HTH!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM