XML technologies / XML / Python & XML / XML basics / Canonical XML
(Excerpt from "Python & XML" by Christopher A. Jones & Fred L. Drake, Jr.)
The term Canonicalization was originally borrowed from its earlier context to indicate that the structure of a document instance matches a master or generally accepted structure of the document. Canonicalization is sometimes abbreviated C14N, similar to the more common I18N for internationalization (Internationalization).
Canonical XML is an emerging W3C Recommendation, allowing you to determine directly whether one physical representation of a document is equivalent to another representation of the same document, and thus whether they are canonically equivalent. This section explores some technical properties of Canonical XML to help you understand how to apply it to your needs.
The Canonical XML data model
To begin converting a document to canonical form, you, or rather your Canonical XML processor, must start with XML in a form it can handle. The first parameter to a canonical translator should therefore be an XPath node-set or a serialized XML document. The second parameter is a Boolean value indicating whether comments should be parsed.
A node-set must have normalized line feeds and attribute values, character data in place of CDATA sections, and resolved character references and parsed entity references. In other words, every node must be thoroughly processed. Neither missing entities nor excess whitespace is allowed. All whitespace inside a root element must be retained, except for changes needed to normalize line delimiters. The whole approach makes the document seem flattened and stretched like pizza dough just before baking.
Document order
Although Canonical XML builds on XPath, certain rules must be followed for XPath node-sets passed to a Canonical XML processor.
The namespace and attribute nodes of an element must follow that element but precede all its child elements.
Namespace nodes must precede attribute nodes.
An element’s namespace nodes are sorted lexically by local name.
An element’s attribute nodes are sorted lexically, using the namespace URI as the primary key and the local name as the secondary key.
Canonical XML structure
Canonical XML no longer needs XML declaration or DTDs and also normalizes whitespace outside the root element. Abbreviated empty elements, such as <element-name/>) are converted to pairs of start and end tags (<element-name></element-name>). Namespaces and attributes may be reordered lexically to meet the canonical requirements described under “Document order” above. In addition, a canonical representation replaces CDATA sections with their actual characters and replaces character references, if necessary. Attribute values and text also contain special characters that are replaced with references.
Canonical XML is quite new, and there is not yet much Python software for processing it. Its broader vision is unclear, but it offers a way to examine two instances, independently of DTD or schema, and dissect them like dead fish to see whether they have the same skeleton. PyXML version 0.7 will support representing XML in canonical form. (Canonical XML may play a role in digital signatures for XML documents, since many syntactically different XML documents can have the same semantic content. — Translator’s note.)
Copyright © 2002 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "Python & XML" is subject to the same conditions as the printed edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de