This [[Module Library|XQuery Module]] contains functions to perform validations against [http://www.w3.org/XML/Schema DTDs, XML Schema] and [http://en.wikipedia.org/wiki/Document_Type_Declaration Document Type Declarations]RelaxNG.This module has been introduced The documentation further describes how to use Schematron validation with {{Version|7.2.2}}BaseX.
=Conventions= All functions and errors in this module are preceded by assigned to the <code>validation<nowiki>http://basex.org/modules/validate</nowiki></code> namespace, which is statically bound to the {{Code|validate}} prefix.<br/> =DTD Validation= Checks whether an XML document validates against a DTD. The input document can be specified as: * {{Code|xs:string}}, representing a URI (relative URIs will always be resolved against the static base URI of the query),* {{Code|xs:string}}, representing the resource in its string representation, or* {{Code|node()}}, representing the resource itself. If no DTD is supplied in a function, the XML document is expected to contain an embedded DTD doctype declaration. ==validate:dtd== {| width='100%'|- valign="top"| width='120' | '''Signature'''|<pre>validate:dtd( $input as item(), $schema as xs:string? := ()) as empty-sequence()</pre>|- valign="top"| '''Summary'''|Validates the XML {{Code|$input}} document against a {{Code|$schema}} and returns an empty sequence or an error.|- valign="top"| '''Errors'''|{{Error|error|#Errors}} the validation fails.<br/>{{Error|init|#Errors}} the validation process cannot be started.<br/>{{Error|not-found|#Errors}} no DTD validator is available.|- valign="top"| '''Examples'''|* {{Code|validate:dtd('doc.xml', 'doc.dtd')}} validates the document {{Code|doc.xml}} against the specified DTD file {{Code|doc.dtd}}.* The following example validates an invalid document against a DTD, which is linked to specified as string:<pre lang='xquery'>try { let $doc := <invalid/> let $schema := '<!ELEMENT root (#PCDATA)>' return validate:dtd($doc, $schema)} catch validate:error { 'DTD Validation failed.'}</pre>|} ==validate:dtd-info== {| width='100%'|- valign="top"| width='120' | '''Signature'''|<pre>validate:dtd-info( $input as item(), $schema as xs:string? := ()) as xs:string*</pre>|- valign="top"| '''Summary'''|Validates the XML {{Code|$input}} document against a {{Code|$schema}} and returns warnings, errors and fatal errors in a string sequence.|- valign="top"| '''Errors'''|{{Error|init|#Errors}} the statically declared validation process cannot be started.<br/>{{Error|not-found|#Errors}} no DTD validator is available.|- valign="top"| '''Examples'''|* <code>httpvalidate:dtd-info(<invalid/>, '<!ELEMENT root (#PCDATA)>')</basexcode> returns:<br/><code>2:11: Element type "invalid" must be declared.org</modulescode>.|} ==validate:dtd-report== {| width='100%'|- valign="top"| width='120' | '''Signature'''|<pre>validate:dtd-report( $input as item(), $schema as xs:string? := ()) as element(report)</pre>|- valign="top"| '''Summary'''|Validates the XML {{Code|$input}} document against a {{Code|$schema}} and returns warnings, errors and fatal errors as XML.|- valign="top"| '''Errors'''|{{Error|init|#Errors}} the validationprocess cannot be started.<br/>{{Error|not-found|#Errors}} no DTD validator is available.|- valign="top"| '''Examples'''|* <code>validate:dtd-report(<invalid/>, '<!ELEMENT root (#PCDATA)>')</code> namespacereturns:<pre lang="xml"><report> <status>invalid</status> <message level="Error" line="2" column="11">Element type "invalid" must be declared.</message></report></pre>|} =XML Schema Validation= Checks whether an XML document validates against an XML Schema. The input document and the schema can be specified as: * {{Code|xs:string}}, containing the path to the resource,* {{Code|xs:string}}, containing the resource in its string representation, or* {{Code|node()}}, containing the resource itself. If no schema is given, the input is expected to contain an {{Code|xsi:(noNamespace)schemaLocation}} attribute, as defined in [https://www.w3.org/TR/xmlschema-1/#xsi_schemaLocation W3C XML Schema]. Different XML Schema processors are supported: * By default, the '''Java implementation''' of XML Schema 1.0 is used (it is based on an old version of Apache Xerces).* The latest version of '''Xerces2''' provides implementations of XML Schema 1.0 and 1.1. The processor will be applied if you download one of the [https://xerces.apache.org/mirrors.cgi#binary binary distributions] and copy the following libraries to the {{Code|lib/custom}} directory of the full distribution of BaseX:** {{Code|org.eclipse.wst.xml.xpath2.processor_1.2.0.jar}}** {{Code|cupv10k-runtime.jar}}** {{Code|xercesImpl.jar}}** {{Code|xml-apis.jar}}* '''Saxon Enterprise Edition''' will be used if you download the [https://www.saxonica.com/download/java.xml ZIP release] and if you copy {{Code|saxon9ee.jar}} and a valid license key to the classpath.
==validate:xsd==
{{Announce|Updated with Version 11:}} caching feature added. {|-width='100%'| - valign='"top' "| width='90120' | '''SignaturesSignature'''|<code><bpre>validate:xsd</b>( $input as item(), $schema as item()? := (), $options as map(*)? := map { }) as empty-sequence()</codepre>|- valign="top"| '''Summary'''|Validates the XML {{Code|$input}} document against a {{Code|$schema}}. The supported {{Code|$options}} are:* {{Code|cache}}: Generated schemas are cached and reused.* All other options are handled as features and passed on to the validator.|- valign="top"| '''Errors'''|{{Error|error|#Errors}} the validation fails.<br/>{{Error|init|#Errors}} the validation process cannot be started.<br />{{Error|not-found|#Errors}} no XML Schema validator is available.|- valign="top"| '''Examples'''|* Pass on document and schema as nodes:<pre lang='xquery'>let $doc := <simple:root xmlns:simple='http://basex.org/simple'/>let $schema := <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' targetNamespace='http://basex.org/simple'> <codexs:element name='root'/> <b/xs:schema>return validate:xsd($doc, $schema)</pre>* Validate all documents of a database against the specified schema, using the supplied feature:<pre lang='xquery'>for $city in db:get('cities')return validate:xsd($city, 'city.xsd', map { 'http://javax.xml.XMLConstants/feature/secure-processing': true() })</bpre>|} ==validate:xsd-info== {| width='100%'|- valign="top"| width='120' | '''Signature'''|<pre>validate:xsd-info( $input as item(), $schema as xsitem()? :string= () , $options as empty-sequencemap(*)? := map { }) as xs:string*</codepre>|-valign="top"| '''Summary'''|Validates the XML {{Code|$input}} document against a {{Code|$schema}} and returns warnings, errors and fatal errors in a string sequence. See {{Function||validate:xsd}} for more information of the {{Code|$options}} parameter.| - valign="top"| '''Errors'''|{{Error|init|#Errors}} the validation process cannot be started.<br/>{{Error|not-found|#Errors}} no XML Schema validator is available.|} ==validate:xsd-report== {| width='100%'|- valign="top"| width='120' | '''SummarySignature'''|Validates the document specified by <codepre>validate:xsd-report( $input as item(), $schema as xs:string? := (), $options as map(*) := map { }) as element(report)</codepre>|- valign="top"| '''Summary'''|Validates the XML {{Code|$input}} document against a {{Code|$schema}} and returns warnings, errors and fatal errors as XML. See {{Function||validate:xsd}} for more information of the {{Code|$options}} parameter.|- valign="top"| '''Errors'''|{{Error|init|#Errors}} the validation process cannot be started.<br />{{Error|not-found|#Errors}} no XML Schema validator is available.|} ==validate:xsd-processor== {| width='100%'|- valign="top"| width='120' | '''Signature'''|<codepre>$inputvalidate:xsd-processor() as xs:string</codepre> may be * |- valign="top"| '''Summary'''|Returns the name of the applied XSD processor.|} ==validate:xsd-version== {| width='100%'|- valign="top"| width='120' | '''Signature'''|<codepre>validate:xsd-version() as xs:string</codepre>|- valign="top"| '''Summary'''|Returns the supported version of XSD Schema.|} =RelaxNG Validation= Checks whether an XML document validates against a RelaxNG schema. The input document and the schema can be specified as: * {{Code|xs:string}}, containing the path to the documentresource,* <code>{{Code|xs:string</code>}}, containing the document resource in its string representation, or* <code>{{Code|node()</code>}}, containing the actual documentresource itself.<code>$input<RelaxNG validation will be available if [https://relaxng.org/jclark/code> jing.html Jing] exists in the classpath. The latest version, {{Code|jing-20091111.jar}}, is required to contain an <code>xsi:(noNamespace)schemaLocation</code> as defined included in the full distributions of BaseX. As Jing additionally supports [http://www.w3nvdl.org/TR/xmlschema-1/#xsi_schemaLocation W3C XML SchemaNVDL]validation, if no you can also use the functions to validate the input against NVDL schemas. ==validate:rng== {| width='100%'|- valign="top"| width='120' | '''Signature'''|<codepre>validate:rng( $input as item(), $schema as item(), $compact as xs:boolean? := map { }) as empty-sequence()</codepre> is given|- valign="top"| '''Summary'''|Validates the XML {{Code|$input}} document against a {{Code|$schema}}, using the XML or {{Code|$compact}} notation.|- valign="top"| '''Errors'''|{{Error|error|#Errors}} the validation fails.<br /><code>$schema{{Error|init|#Errors}} the validation process cannot be started.<br/code> can be used to optionally specify {{Error|not-found|#Errors}} the location of the XSD file for validationRelaxNG validator is not available.|-| valign='"top' "| '''ExceptionsExamples'''| Todo* {{Code|validate:rng('doc.xml', 'doc.rng')}} validates the document {{Code|doc.xml}} against the specified schema {{Code|doc.rng}}.
|}
==validate:dtdrng-info== {|width='100%'|-| valign='"top' "| width='90120' | '''SignaturesSignature'''|<code><bpre>validate:dtd</b>rng-info( $input as item(), $schema as item() , $compact as empty-sequence(xs:boolean? := map { })as xs:string*</codepre>|- valign="top"| '''Summary'''|Validates the XML {{Code|$input}} document against a {{Code|$schema}}, using the XML or {{Code|$compact}} notation, and returns warnings, errors and fatal errors in a string sequence.|- valign="top"| '''Errors'''|{{Error|init|#Errors}} the validation process cannot be started.<br />{{Error|not-found|#Errors}} the RelaxNG validator is not available.|} ==validate:rng-report== {| width='100%'|- valign="top"| width='120' | '''Signature'''|<code><bpre>validate:dtd</b>rng-report( $input as item(), $dtd schema as xs:string, $compact as xs:boolean? := map { }) as empty-sequenceelement(report)</codepre>|-valign="top"| '''Summary'''|Validates the XML {{Code|$input}} document against a {{Code|$schema}}, using the XML or {{Code|$compact}} notation, and returns warnings, errors and fatal errors as XML.|- valign="top"| '''Errors'''|{{Error|init|#Errors}} the validation process cannot be started.<br/>{{Error|not-found|#Errors}} The RelaxNG validator is not available.
|}
==Examples=Schematron Validation=
'''Example 1If you want to use Schematron for validating documents, install Vincent Lizzi’s excellent [https://github.com/Schematron/schematron-basex Schematron XQuery Module for BaseX]: Basic validation against a document containing a schema definition'''
'''Query:'''<pre classlang="brush:'xquery"'>validaterepo:xsdinstall($doc'https://github.com/Schematron/schematron-basex/raw/master/dist/schematron-basex-1.2.xar')
</pre>
The following query illustrates how documents are validated. It is directly taken from the GitHub project:
<pre lang='xquery'>
import module namespace schematron = "http://github.com/Schematron/schematron-basex";
let $sch := schematron:compile(doc('rules.sch'))
let $svrl := schematron:validate(doc('document.xml'), $sch)
return (
schematron:is-valid($svrl),
for $message in schematron:messages($svrl)
return concat(schematron:message-level($message), ': ', schematron:message-description($message))
)
</pre>
=Errors=
{| class="wikitable" width="100%"
! width="110"|Code
|Description
|- valign="top"
|{{Code|error}}
|The document cannot be validated against the specified schema.
|- valign="top"
|{{Code|init}}
|The validation cannot be started.
|- valign="top"
|{{Code|not-found}}
|No validator is available.
|}
=Changelog=
;Version 11.0
* Updated: {{Function||validate:xsd}}, {{Function||validate:xsd-info}}, {{Function||validate:xsd-report}}: caching feature added
;Version 9.2
* Added: {{Function||validate:xsd-processor}}, {{Function||validate:xsd-version}}
* Updated: {{Function||validate:xsd}}, {{Function||validate:xsd-info}}, {{Function||validate:xsd-report}}: version argument was dropped (the latest version will always be used)
;Version 9.0
* Updated: error codes updated; errors now use the module namespace
;Version 8.5
* Updated: Relative URIs will always be resolved against the static base URI of the query
;Version 8.3
* Added: {{Function||validate:rng}}, {{Function||validate:rng-info}}
* Added: {{Function||validate:dtd-report|dtd-report}}, {{Function||validate:xsd-report}}, {{Function||validate:rng-report}}
;Version 7.6
* Added: {{Function||validate:xsd-info}}, {{Function||validate:dtd-info}}
[[Category:XQuery]]The module was introduced with Version 7.3.