Previous Up Next


p:validate-with-xml-schema

p:validate-with-xml-schema — Validates a document using W3C XML Schema.

Synopsis

<p:declare-step type="p:validate-with-xml-schema">
     <p:input port="source" primary="true"/>
     <p:input port="schema" sequence="true"/>
     <p:output port="result"/>
     <p:option name="use-location-hints" select="'false'"/>        <!-- boolean -->
     <p:option name="try-namespaces" select="'false'"/>            <!-- boolean -->
     <p:option name="assert-valid" select="'true'"/>               <!-- boolean -->
     <p:option name="mode" select="'strict'"/>                     <!-- "strict" | "lax" -->
</p:declare-step>

Description

The p:validate-with-xml-schema step applies [W3C XML Schema: Part 1] validity assessment to its input document. If assert-valid is true, the step fails if the document is invalid, otherwise a copy of the input appears on the output, possibly representing an augmented PSVI.

Validation is performed against the set of schemas represented by the documents on the schema port. These schemas must be used in preference to any schema locations provided by schema location hints encountered during schema validation, that is, schema locations supplied for xs:import or xsi:schema-location, or determined by schema-processor-defined namespace-based strategies, for the namespaces covered by the documents available on the schemas port.

If xs:include elements occur within the supplied schema documents, they are treated like any other external documents (see Section 7, “External documents, step outputs, and caching”). It is implementation-defined if the documents supplied on the schemas port are considered when resolving xs:include elements in the schema documents provided.

The use-location-hints and try-namespaces options allow the pipeline author to control how the schema processor should attempt to locate schema documents necessary but not provided on the schema port. Any schema documents provided on the schema port are used in preference to schema documents located by other means.

If the use-location-hints option is “true”, the processor should make use of schema location hints to locate schema documents. If the option is “false”, the processor should ignore any such hints.

If the try-namespaces option is “true”, the processor should attempt to dereference the namespace URI to locate schema documents. If the option is “false”, the processor should not dereference namespace URIs.

The mode option allow the pipeline author to control how schema validation begins. It must be either “strict” or “lax”. The “strict” mode means that the document element must be declared and schema-valid, otherwise it will be treated as invalid. The “lax” mode means that the absence of a declaration for the document element does not itself count as an unsuccessful outcome of validation.

If the assert-valid option is false, it is not an error for the document to be invalid. In this case, if the implementation does not support the PSVI, p:validate-with-xml-schema is essentially just an “identity” step, but if the implementation does support the PSVI, then the resulting document will have additional type information (at least for the subtrees that are valid).

The result of the assessment is a document with the Post-Schema-Validation-Infoset (PSVI) ([W3C XML Schema: Part 1]) annotations, if the pipeline implementation supports such annotations. If not, the input document is reproduced with any defaulting of attributes and elements performed as specified by the XML Schema recommendation.

Errors

Error Description
err:C0053 Occurs if the assert-valid option is true and the input document is not valid.
err:C0055 Occurs if a mode is specified and the implementation does not support the specified mode.

Examples

The simplest use of p:validate-with-xml-schema simply validates a document.

  1 <p:pipeline xmlns:p="http://www.w3.org/ns/xproc" name="main"
                version="1.0">
    
      <p:validate-with-xml-schema>
  5     <p:input port="schema">
          <p:document href="../docs/document.xsd"/>
        </p:input>
      </p:validate-with-xml-schema>
    </p:pipeline>
Input Output
1 <doc>
<title>Title</title>
<p>Some paragraph.</p>
</doc>
 
1 <doc>
<title>Title</title>
<p>Some paragraph.</p>
</doc>

FIXME: more examples, showing all the various options and what they do.

XML Schema output is a PSVI which may include both defaulted attributes and type information. Here we see a default attribute added to the div element.

  1 <p:pipeline xmlns:p="http://www.w3.org/ns/xproc" name="main"
                version="1.0">
    
      <p:validate-with-xml-schema>
  5     <p:input port="schema">
          <p:document href="../docs/document.xsd"/>
        </p:input>
      </p:validate-with-xml-schema>
    </p:pipeline>
Input Output
1 <doc>
<title>Title</title>
<div>
<p>Some paragraph.</p>
5 </div>
</doc>
 
1 <doc>
<title>Title</title>
<div class="normal">
<p>Some paragraph.</p>
5 </div>
</doc>

Type information doesn't show up in the serialized output, but it can be used in the pipeline, as we see here:

  1 <p:pipeline xmlns:p="http://www.w3.org/ns/xproc" name="main"
                version="1.0">
    
      <p:identity>
  5     <p:input port="source" select="//element(*,xs:anyURI)"/>
      </p:identity>
    
      <p:wrap-sequence name="wrapper1"
                       wrapper="uris-before-validation"/>
 10 
      <p:validate-with-xml-schema>
        <p:input port="source">
          <p:pipe step="main" port="source"/>
        </p:input>
 15     <p:input port="schema">
          <p:document href="../docs/document.xsd"/>
        </p:input>
      </p:validate-with-xml-schema>
    
 20   <p:identity>
        <p:input port="source" select="//element(*,xs:anyURI)"/>
      </p:identity>
    
      <p:wrap-sequence name="wrapper2"
 25                    wrapper="uris-after-validation"/>
    
      <p:wrap-sequence wrapper="uris">
        <p:input port="source">
          <p:pipe step="wrapper1" port="result"/>
 30       <p:pipe step="wrapper2" port="result"/>
        </p:input>
      </p:wrap-sequence>
    </p:pipeline>
Input Output
1 <doc>
<title>Title</title>
<div>
<p>Some paragraph
5 with a uri: <uri>http://example.com/</uri>.</p>
</div>
</doc>
 
1 <uris>
<uris-before-validation/>
<uris-after-validation>
<uri>http://example.com/</uri>
5 </uris-after-validation>
</uris>