module namespace testxsd = "http://www.architextus.com/xquery/library/how2_testxsd"; declare namespace rest="http://exquery.org/ns/restxq"; (: Ensures that only one validation process will run at the same time. :) declare option query:write-lock "validation"; declare %rest:path("/testxsd/test") %rest:GET %output:method("xhtml") %output:html-version("5.0") function testxsd:test() as element(html) { let $xml := db:open('DebugXsdRef', 'images.xml') let $xsd := db:open('DebugXsdRef', 'images.xsd') let $errors := testxsd:validate($xml, $xsd) return
{ if (count($errors)>0) thenValidation successful.
} }; (:~ : This function validates an XML document against an XML Schema file. : The specified schema file is expected to be originally stored in : the database. :) declare %private function testxsd:validate( $xml as document-node(), $xsd as document-node(element(xs:schema))) as element(li)? { (: copy schema files from database to temporary directory :) $xsd//xs:include/@schemaLocation ! file:write(., db:open(db:name($xsd), .)), (: validate document :) try { validate:xsd($xml, $xsd) } catch * {File '{ db:path($xml) }' doesn't validate against the '{ db:path($xsd) }' schema.
{'Error [' || $err:code || ']: ' || $err:description}