XML technologies / XML Schema / Using and developing schemas / What schemas do for XML

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

An XML schema language formalizes the constraints applying to a class of XML documents as rules or structural models. Schemas often serve as design tools, establishing the framework on which implementations can build. Formalization is essential for software designers, so formalizing XML instance constraints and structures leads to widely differing applications. New uses appear daily, but most fall into validation, documentation, querying, binding or editing.

Validation

Validation is the most common schema application in XML. Reasons and occasions to validate include receiving a document, importing data into a traditional system, creating or manually changing a document, and testing an application’s output. In each case a schema handles a substantial part of the task. Different schemas provide different validation. Some particularly complex rules are better expressed as procedural code, but validation is generally the original and main purpose of a schema.

Validation can act as a firewall against XML’s diversity. Such firewalls are mainly useful in two situations: at external boundaries, when receiving documents from outside, as with web services and other XML communication; and at defined handoff points in processing designed as a sequence of transformations. Validating against a schema ensures that content follows expected rules and simplifies the programs needed to process it.

Validation can greatly reduce the risks of processing XML from sources beyond our control. It does not remove the need to follow the chosen protocol’s management rules or write robust applications, but adds a useful checking layer between the communication interface and your program.

Validation has several levels. Structural validation checks that element and attribute structures meet requirements, but says little about their text. Data validation examines that content and checks the kinds of information expected. Other validation, often called business rules, may check relationships between information and higher-level plausibility. These operations are usually implemented procedurally rather than through schema validation.

XML is a good foundation for transformation pipelines using widely available tools. Every transformation risks errors, and errors are easier to fix near their source, so it is sensible to add validation checkpoints to the pipeline. For some applications validation after every step is prohibitively expensive; for others it is vital to notice errors immediately, before they do damage and while still easy to identify. Different situations require different validation, and validating more frequently during pipeline development than in production may be sensible.

Documentation

XML schemas often document a vocabulary even when validation is unnecessary. They formally describe it with precision and clarity difficult to achieve in prose. Publishing a new XML vocabulary specification without some form of XML schema is highly unusual.

Machine readability benefits schemas as documentation in several ways. A human-readable version can be generated from their formal description. Schema IDEs, for instance, generate diagrams helping explain document structure. Developers can also write XSLT transformations generating structural descriptions. This technique produced the structure of the XML Schema element reference and the data type reference from the W3C XML Schema for W3C XML Schema published on the W3C website.

In Documenting schemas, we shall see that W3C XML Schema includes further ways to annotate schemas with structured and unstructured information, making explicit documentation uses easier.

Query support

The first versions of XPath and XSLT were designed to work without explicit knowledge of document structure. This succeeded but limited performance and functionality. Knowing the structure could improve optimizers, while a type system could improve functions such as sorting and equality tests. XPath and XSLT version 2, and the first XQuery version, a developing specification for an XML query language, will rely on a W3C XML Schema to offer these capabilities.

Data binding

Although writing XML-processing applications with SAX, DOM and similar APIs is not particularly hard, it is low-level, tedious and error-prone. Writing and maintaining such programs quickly becomes expensive as vocabulary elements and attributes multiply. Automating this by binding XML information directly to application structures, usually objects or RDBMS tables, is probably an idea as old as markup languages themselves.

Ronald Bourret, who maintains a list of XML data binding resources, distinguishes design-time and runtime binding tools. Runtime tools generate bindings by introspecting document and application structures. Design-time tools instead use a model formalized in some schema. He describes this category as usually more flexible in the mappings it supports.

Many languages, both specialized and general-purpose XML schema languages, define such bindings. W3C XML Schema has considerable momentum here. Many data binding tools began supporting W3C XML Schema from early specification drafts, before it was finalized.

Guided editing

XML editors, and previously SGML editors, have long used schemas to offer suitable choices while creating and editing documents. DTDs provided structural information; newer XML schema languages add more sophisticated information on structures and data types.

The W3C is currently creating a standard API through which guided-editing applications can ask a schema processor which operation is possible at a given document position, for example, “Can I insert this new element here?” or “Can I set this text node to this value?” The Document Object Model (DOM) Level 3 Abstract Schemas and Load and Save Specification, still under development, defines abstract schemas broadly enough to cover both DTDs and W3C XML Schema, and in principle other schema languages. Once finalized and widely adopted, this API should allow any schema processor to integrate with any editor application.

Another approach generates editors from schema information. Combined with presentation and control information, these tools let users edit XML using applications tailored to a specific schema. For example, the developing W3C XForms specification proposes separating form logic and presentation from the structure of the edited data. That structure then comes from a W3C XML Schema .

   

 

 

 

back to top