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:
- W3C XML Schema (XSD):
http://www.w3.org/2001/XMLSchema - ISO Schematron:
http://purl.oclc.org/dsdl/schematron
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:
- Phase 1 (Structural Validation): The processor
evaluates
schema.xsdvia thehttp://www.w3.org/2001/XMLSchemanamespace to ensure elements, attributes, datatypes, and order follow the structural definition. - Phase 2 (Business Rule Validation): The processor
loads
rules.schvia thehttp://purl.oclc.org/dsdl/schematronnamespace 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.