XML technologies / XML / Python & XML / XML basics / Document types and schemas

(Excerpt from “Python & XML” by Christopher A. Jones & Fred L. Drake, Jr.)

A document type resembles a type in a programming language. Both describe structures that can be combined in particular ways. Their basic components and permitted combinations differ, but the concepts are comparable. A document type is usually called a schema. In many applications it differs little from a database schema, though that similarity need not matter. We often use “schema” when the definition mechanism is unimportant, since “document type” historically suggests a particular schema language.

Schemas are valuable for several reasons, especially two: they make us think carefully about applications and data, and specify how documents should be built and interpreted across organizational boundaries. This can be crucial in supply-chain integration, where automated exchange of dynamically generated documents may carry contractual obligations. Everyone must agree on meaning: misinterpretation can be costly.

Document types build on data types and structural rules. Data types closely resemble primitive programming-language types. Schema languages offer different sets of types, sometimes extensible, sometimes allowing arbitrary external type systems rather than defining their own. Some let you type any document content; others restrict typing to specific constructs.

Every schema language defines permitted element order and nesting, and associates attributes with element types. Everything else may vary. Understand the differences and choose according to application requirements, available tools and necessary interoperability.

Document type definitions

XML 1.0 specifies a particular way to define document types, known as a document type definition or DTD. The language specifying a DTD is actually part of XML itself, informally called the DTD language. It is an XML subset with slightly different syntax, not permitting arbitrary content mixed with markup.

XML’s DTD language derives from that of SGML but omits less-used constructs for simplicity, both in the language and its tools. Those omissions matter only if you know SGML DTDs, so this book does not cover them. Consult the XML Recommendation and specialist document-type design books for the differences.

We examine DTD construction and interpretation later. Though DTDs allow flexible element composition, they define few attribute data types and almost no extension mechanism. Despite those limits, they remain important thanks to early inclusion in XML 1.0, similarity to SGML DTDs, plentiful tools and relative ease of learning and use.

Alternative schema languages

The XML sublanguage specifying document types largely came from SGML and may be the specification’s least appreciated aspect. Useful though it is in some projects, it is a compromise. Better than mere application-defined well-formed XML, it nevertheless leaves demand for something better. One size does not fit all standards, so alternative document-type languages have emerged, collectively called schema languages.

Scope, complexity and tool availability differ. We consider popular languages, their intended uses and Python support. They share two features: XML syntax and namespace awareness, allowing schemas to include elements and attributes from several namespaces. Both differ fundamentally from DTDs and can reasonably be considered major improvements.

XML Schema

The World Wide Web Consortium (W3C) worked to standardize a schema language suitable for everyone; XML Schema is the result. Like many committee designs, it has prompted widespread dissatisfaction with XML Schema, not for lack of power but because many find it too complex. It describes permitted document structures and element and attribute data types more precisely and flexibly than DTDs.

XML Schema defines both document and data types, with basic types to build upon: numbers, strings constrained by regular expressions, and more complex dates and times. These types are far richer than DTD types. Schemas can constrain attributes and element content accordingly, describing large document types much more precisely. Validation tools can then eliminate much application-specific error-handling code. XML Schema types are briefly used in Python, web services and SOAP but not explained in detail.

A Python XML Schema validator exists; see More Python XML tools for details.

TREX

Tree Regular Expressions for XML (TREX) is a schema language designed by James Clark, active in useful XML standards since XML’s beginnings and known for earlier SGML contributions. TREX does not define fine-grained types like XML Schema, instead using externally specified types, potentially including XML Schema’s.

PyXML includes a TREX validator in the module xml.schema.trex, added in PyXML 0.7.0.

RELAX NG

RELAX NG combines two well-received languages, TREX and RELAX. Its specification is still under active development at writing time. It is a joint effort by James Clark and Makoto Murata, authors of TREX and RELAX, sponsored by the Organization for the Advancement of Structured Information Standards (OASIS). It follows TREX’s approach to data types. Full information is available at OASIS RELAX NG TC. An alternative non-XML syntax has also been proposed.

Schematron

Rick Jelliffe’s Schematron assertion language differs from other schema languages. Rather than defining allowed elements and their content and attribute models, Schematron makes assertions about relationships between elements and attributes.

  

 

 

 

back to top