XML technologies / XML Schema / Building blocks / Other alternatives

(Excerpt from “XML Schema” by Eric van der Vlist)

Elements xs:include and xs:redefine safely include schema fragments, with processing designed to produce a coherent schema. The cost is rigidity: only complete documents, at global level, can be included, not selected definitions or local elements such as annotations or common facets. Suppose all our date-related data types must specify time zones, and we developed a general pattern enforcing this:

<xs:pattern value=".+(Z|[+-].{5})"/>

We could derive types for all eight primitive temporal formats and require designers to use them. Or we might provide a reusable pattern reference, allowing changes without updating every schema. Then xs:include and xs:redefine cannot help: general XML inclusion, external parsed entities and XInclude, must be considered.

External parsed entities

External parsed entities are among XML’s DTD-inherited SGML features. They are entities, declared in the DTD and referenced later, external because replacement text comes from a file, and parsed because content is parsed into the receiving document’s infoset.

Create an XML document holding the pattern:

<?xml version="1.0"?>
<xs:pattern value=".+(Z|[+-].{5})"
xmlns:xs="http://www.w3.org/2001/XMLSchema"/>

A namespace declaration in this external parsed entity is unnecessary if receiving documents always declare the namespace with the same prefix. Redeclaration is allowed but ineffective in that case. Including it nevertheless ensures recognition as belonging to W3C XML Schema even if another prefix is used. Declare the entity in the schema’s internal or external DTD, then reference it in derivations:

<?xml version="1.0"?>
<!DOCTYPE xs:schema[
<!ENTITY TZ-pattern SYSTEM "pattern.ent">
]>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:simpleType name="myDate">
      <xs:restriction base="xs:date">
         &TZ-pattern;
      </xs:restriction>
   </xs:simpleType>
   <xs:element name="myDate" type="myDate"/>
</xs:schema>

This offers finer granularity than schema inclusion, which handles only global components. General inclusion through external entities or XInclude lacks W3C XML Schema semantics and cannot redefine. Other DTD features, internal parsed entities and even parameter entities, can combine with W3C XML Schema for innovative results.

XInclude

XInclude is an XML application based on XPointer, currently a W3C Candidate Recommendation. It will eventually replace external parsed entities, with similar use but no advance reference declaration and support for including fragments. The same example can use XInclude to retrieve the pattern by its id, even from a fuller schema:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:simpleType name="date">
      <xs:restriction base="xs:date">
         <xs:pattern value=".+(Z|[+-].{5})" id="TZ-pattern"/>
      </xs:restriction>
   </xs:simpleType>
   <xs:element name="myDate" type="myDate"/>
</xs:schema>

Because attribute id of element xs:pattern is defined, XPointer bare-name notation, “bare names”, uses an id value as fragment identifier. Here, the XPointer reference to xs:pattern is pattern.xsd#TZ-pattern. We can write:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
   xmlns:xi="http://www.w3.org/2001/XInclude">
   <xs:simpleType name="myDateTime">
      <xs:restriction base="xs:dateTime">
         <xi:include href="pattern.xsd#TZ-pattern" parse="xml"/>
      </xs:restriction>
   </xs:simpleType>
   <xs:element name="myDate" type="myDateTime"/>
</xs:schema>

XInclude is still developing, so this notation may change before it becomes a W3C Recommendation. Reading this schema also requires an XInclude-aware parser.

   

 

 

 

back to top