0

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
4

1 回答 1

0

简短的回答,检查是否Schemas被选为您的myDatabase文档数据库的模式数据库。

您评估的应用服务器决定了应该使用哪些文档和模块数据库。文档数据库反过来决定了哪些模式和触发器数据库与之配套。如果您在管理 UI 中创建一个空数据库,它会(none)默认设置架构和触发器。同样可能适用于创建数据库的其他方法。

于 2019-12-06T06:37:16.450 回答