Presents your
XML E-NEWSLETTER for November 20, 2002
<------------------------------------------->
OVERCOME VARIATIONS WITH THE CANONICAL FORM
Using XML documents can sometimes pose problems because of the variation
in acceptable XML formats. Differences in namespace declarations,
spacing, parsed entities, and other aspects can make documents that are
actually equivalent appear very different. Canonical XML aims to solve this
problem by providing a method for converting everyday XML documents into
canonical form.
THE PROBLEM
The central issue that Canonical XML solves is the syntactic variation
allowed in XML documents. For example, the following two fragments are
equivalent:
As you can see, they look different but communicate the same information--a
tag named MyTag that has no attributes, no content, and no child
elements. There are several examples throughout XML syntax that allow
variations such as this and which make it difficult to verify if two
documents are logically the same.
THE SOLUTION
In order to perform verification, it's important to create a standard
physical representation for the XML documents. The solution is to create a
canonicalized XML document from the original document. The canonical form
specifies normalizations and constraints that are not specified in
vanilla XML implementations. While all Canonical XML documents are still XML
documents, not all XML documents are Canonical XML documents.
CANONICAL XML DEFINED
There are really only a few rules that define the difference between a
standard XML document and a canonical XML document. The rules are defined
by the W3C in its Canonical XML recommendation from March 2001.
Highlighted below are a few of the major differences between XML and
Canonical XML.
UTF ENCODING
Unicode Transformation Format (UTF) encoding is a character encoding
used to define the way the characters in an XML document are interpreted.
Normally, XML documents can be in a variety of character encodings;
however, Canonical XML specifies that all documents must use UTF.
Without going into too much detail, UTF is a character encoding that allows
for multi-octet characters. UTF provides the ability to use the characters
beyond the standard US-ASCII character set while transparently supporting
US-ASCII.
EMPTY ELEMENTS CONVERTED
With Canonical XML, empty elements are converted to the appropriate
start and end tags. In addition, extraneous white space within the star- and
end-tags is removed, and element attributes are put in lexicographical
order. Look at the following snippet:
The above snippet would result in the following Canonical XML snippet:
ENTITY REFERENCES REPLACED
Standard XML documents allow data to be represented as entities;
however, in Canonical XML, entities must be parsed and replaced with actual
data. For example, an entity such as &MyEntity; must be replaced by the
actual data referenced by the entity.
CDATA REPLACED
All CDATA content must be replaced by the real data with reserved
characters properly represented as character entities. Take, for
example, the following XML snippet:
& & ]]>
Using Canonical XML, we get rid of the CDATA and encode any reserved
characters as follows:
<One> & <two> &
<three>
XML, COMMENTS, DTD DECLARATION REMOVED
With Canonical XML, the XML declaration, the DTD declaration, and XML
comments are removed. The XML declaration is removed because Canonical
XML applies only to XML version 1.0 in its current form; therefore the
document is assumed to be only XML version 1.0.
The above XML snippet would become the following:
There is a "special" version of Canonical XML that includes comments.
Using commented Canonical XML, the above snippet would result in the
following:
LEARN MORE
The above examples show only a piece of the full Canonical XML
differences. You can learn more about Canonical XML by reading the
W3C's recommendation document.
http://www.w3.org/TR/xml-c14n
Brian Schaffner is a senior consultant for Fujitsu Consulting. He
provides architecture, design, and development support for Fujitsu's
Telcom360 group.
----------------------------------------