Link XML to XSD and Schematron with xml-model

Associating an XML document with multiple schemas allows developers to enforce both structural validity and complex business rules simultaneously. By utilizing multiple <?xml-model?> processing instructions, an XML document can explicitly bind to both a W3C XML Schema (XSD) for structural typing and an ISO Schematron file for semantic assertions without modifying the root element attributes.

Using Multiple xml-model Processing Instructions

The xml-model processing instruction is defined by the W3C to associate XML documents with schema resources. Rather than relying on parser-specific attributes like xsi:noNamespaceSchemaLocation, you can place multiple <?xml-model?> directives in the document prolog.

Each schema declaration requires specific pseudo-attributes: * href: The relative or absolute URI locating the schema file. * type: The MIME type of the schema (typically application/xml). * schematypens: The namespace URI that identifies the schema language being used.

Schema Namespace Identifiers (schematypens)

To inform validating parsers of the exact schema engine to invoke, assign the standardized namespace URIs:

Complete Implementation Example

Below is a complete XML document referencing both an XSD file (schema.xsd) and a Schematron file (rules.sch):

<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="schema.xsd" 
            type="application/xml" 
            schematypens="http://www.w3.org/2001/XMLSchema"?>
<?xml-model href="rules.sch" 
            type="application/xml" 
            schematypens="http://purl.oclc.org/dsdl/schematron"?>
<order id="ord-10492">
    <customer>Jane Doe</customer>
    <items>
        <item sku="A12" quantity="2" unitPrice="15.00"/>
    </items>
    <total>30.00</total>
</order>

How Validation Engines Process the Rules

When an xml-model-aware processor reads the document:

  1. Phase 1 (Structural Validation): The processor evaluates schema.xsd via the http://www.w3.org/2001/XMLSchema namespace to ensure elements, attributes, datatypes, and order follow the structural definition.
  2. Phase 2 (Business Rule Validation): The processor loads rules.sch via the http://purl.oclc.org/dsdl/schematron namespace to validate dynamic constraints, such as verifying that the <total> matches the sum of the item quantities multiplied by their prices.

This dual association ensures complete validation coverage while keeping structural grammar separated from contextual business logic.