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)? { let $temp := file:create-temp-dir('validate-', '') return ( (: copy schema files from database to temporary directory :) for $loc in $xsd//xs:include/@schemaLocation let $path := $temp || $loc return ( (: create sub-directories :) file:create-dir(file:dir-name($path)), file:write($path, db:open(db:name($xsd), $loc)) ), try { (: point schema locations to temporary files :) let $xsd := copy $copy := $xsd modify ( for $loc in $copy//xs:include/@schemaLocation return replace value of node $loc with $temp || $loc ) return $copy (: validate document :) return validate:xsd($xml, $xsd) } catch * {File '{ db:path($xml) }' doesn't validate against the '{ db:path($xsd) }' schema.
{'Error [' || $err:code || ']: ' || $err:description}