Please refer to theerrata for this document, which may include some normative corrections.
See alsotranslations.
Copyright © 2008W3C® (MIT,ERCIM,Keio),All Rights Reserved. W3Cliability,trademark anddocumentuse rules apply.
Canonical XML Version 1.1 is a revisionto Canonical XML Version 1.0 to address issues related to inheritance ofattributes in the XML namespace when canonicalizing document subsets,including the requirement not to inheritxml:id
, andto treatxml:base
URI path processing properly.
Any XML document is part of a set of XML documents that are logically equivalent within an application context, but which vary in physical representation based on syntactic changes permitted by XML 1.0[XML] and Namespaces in XML 1.0[Names]. This specification describes a method for generatinga physical representation, the canonical form, of an XML documentthat accounts for the permissible changes. Except for limitationsregarding a few unusual cases, if two documents have the same canonicalform, then the two documents are logically equivalent within thegiven application context. Note that two documents may have differingcanonical forms yet still be equivalent in a given context basedon application-specific equivalence rules for which no generalizedXML specification could account.
Canonical XML Version 1.1 is applicableto XML 1.0 and defined in terms of the XPath 1.0 data model. Itis not defined for XML 1.1.
This section describes the statusof this document at the time of its publication. Other documentsmay supersede this document. A list of current W3C publications andthe latest revision of this technical report can be found in theW3C technical reports index at http://www.w3.org/TR/.
This is aW3C Recommendation.
This document has been reviewed by W3C Members, by software developers, and by other W3C groups and interested parties, and is endorsed by the Director as a W3C Recommendation. It is a stable document and may be used as reference material or cited from another document. W3C's role in making the Recommendation is to draw attention to the specification and to promote its widespread deployment. This enhances the functionality and interoperability of the Web.
Comments on this document should be sent towww-xml-canonicalization-comments@w3.org which is an automaticallyarchived public email list.
Theimplementation report details CR implementation feedback fromseveral implementations. It should be noted that this IR reflectsresults implemented against the CR as clarified based on issues raisedduring the CR period and subsequently reflected in the wording ofthis Recommendation.
This document has been producedby theW3C XML Core WorkingGroup as part of the W3CXML Activity. The authors of this document are the members ofthe XML Core Working Group and invited experts from the Digital Signaturecommunity.
This document was produced by a group operatingunder the5 February 2004 W3C Patent Policy. W3C maintains apublic list of any patent disclosures made in connection withthe deliverables of the group; that page also includes instructionsfor disclosing a patent. An individual who has actual knowledge ofa patent which the individual believes contains Essential Claim(s) must disclose the information in accordancewith section 6 of the W3C Patent Policy.
The English versionof this specification is the only normative version.
The XML 1.0Recommendation[XML] specifies the syntax of aclass of resources called XML documents. The Namespaces in XML 1.0 Recommendation[Names] specifies additional syntax and semanticsfor XML documents. It is possible for XML documents which are equivalentfor the purposes of many applications to differ in physical representation.For example, they may differ in their entity structure, attributeordering, and character encoding. It is the goal of this specificationto establish a method for determining whether two documents are identical,or whether an application has not changed a document, except for transformationspermitted by XML 1.0 and Namespaces in XML 1.0.
Canonical XML Version 1.1is a revision to Canonical XML Version 1.0[C14N10] to addressissues related toinheritance of attributes in the XML namespace when canonicalizingdocument subsets, including the requirement not to inheritxml:id
, and to treatxml:base
URI path processingproperly. See also the Working Group Notes on[C14N-Issues] and[DSig-Usage] forfurther discussion of the relationship of Canonical XML Version 1.1 to CanonicalXML Version 1.0.
Canonical XML Version 1.1 is applicable to XML 1.0 and definedin terms of the XPath 1.0 data model. It is not defined for XML1.1.
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALLNOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL"in this document are to be interpreted as described in RFC 2119[Keywords].
See[Names] for the definition ofQName.
Adocument subset is a portion of an XML document indicatedby a node-set that may not include all of the nodes in the document.
Thecanonical form of an XML document is physical representationof the document produced by the method described in this specification.The changes are summarized in the following list:
xml:base
attributes[C14N-Issues] is performedThe termcanonical XML refers to XML that is in canonicalform. TheXML canonicalization method is the algorithm definedby this specification that generates the canonical form of a givenXML document or document subset. The termXML canonicalization refers to the process of applying the XML canonicalization methodto an XML document or document subset.
The XPath 1.0 Recommendation[XPath] defines the termnode-set and specifiesa data model for representing an input XML document as a set of nodesof various types (element, attribute, namespace, text, comment, processinginstruction, and root). The nodes are included in or excluded froma node-set based on the evaluation of an expression. Within this specification,a node-set is used to directly indicate whether or not each node shouldbe rendered in the canonical form (in this sense, it is used as aformal mathematical set). A node that is excluded from the set isnot rendered in the canonical form being generated, even if its parentnode is included in the node-set. However, an omitted node may stillimpact the rendering of its descendants (e.g. by augmenting the namespacecontext of the descendants or supplying a base URI throughxml:base
).
Since the XML 1.0 Recommendation[XML] and the Namespaces in XML 1.0 Recommendation[Names] define multiple syntactic methods for expressing the same information,XML applications tend to take liberties with changes that have noimpact on the information content of the document. XML canonicalizationis designed to be useful to applications that require the abilityto test whether the information content of a document or documentsubset has been changed. This is done by comparing the canonical formof the original document before application processing with the canonicalform of the document result of the application processing.
For example, a digital signature over the canonical form of an XMLdocument or document subset would allow the signature digest calculationsto be oblivious to changes in the original document's physical representation,provided that the changes are defined to be logically equivalent bythe XML 1.0 or Namespaces in XML 1.0. During signature generation, thedigest is computed over the canonical form of the document. The documentis then transferred to the relying party, which validates the signatureby reading the document and computing a digest of the canonical formof the received document. The equivalence of the digests computedby the signing and relying parties (and hence the equivalence of thecanonical forms over which they were computed) ensures that the informationcontent of the document has not been altered since it was signed.
Note: Although not stated as a requirement on implementations, norformally proved to be the case, it is the intent of thisspecification that if the text generated by canonicalizing adocument according to this specification is itself parsed andcanonicalized according to this specification, the text generated bythe second canonicalization will be the same as that generated bythe first canonicalization.
Two XML documents may have differing information content thatis nonetheless logically equivalent within a given application context.Although two XML documents are equivalent (aside from limitationsgiven in this section) if their canonical forms are identical, itis not a goal of this work to establish a method such that two XMLdocuments are equivalent ifand only if their canonical formsare identical. Such a method is unachievable, in part due to application-specificrules such as those governing unimportant whitespace and equivalentdata (e.g.<color>black</color>
versus<color>rgb(0,0,0)</color>
). There are also equivalencies established by other W3C Recommendationsand Working Drafts. Accounting for these additional equivalence rulesis beyond the scope of this work. They can be applied by the applicationor become the subject of future specifications.
The canonicalform of an XML document may not be completely operational within theapplication context, though the circumstances under which this occursare unusual. This problem may be of concern in certain applicationssince the canonical form of a document and the canonical form of thecanonical form of the document are equivalent. For example, in a digitalsignature application, it cannot be established whether the operationaloriginal document or the non-operational canonical form was signedbecause the canonical form can be substituted for the original documentwithout changing the digest calculation. However, the security riskonly occurs in the unusual circumstances described below, which canall be resolved or at least detected prior to digital signature generation.
The difficulties arise due to the loss of the following informationnot available in thedata model:
In the first case, note that a document containing a relativeURI[URI] is only operational when accessed froma specific URI that provides the proper base URI. In addition, ifthe document contains external general parsed entity references tocontent containing relative URIs, then the relative URIs will notbe operational in the canonical form, which replaces the entity referencewith internal content (thereby implicitly changing the default baseURI of that content). Both of these problems can typically be solvedby adding support for thexml:base
attribute[XBase] to the application, then adding appropriatexml:base
attributes to document element and all top-levelelements in external entities. In addition, applications often havean opportunity to resolve relative URIs prior to the need for a canonicalform. For example, in a digital signature application, a documentis often retrieved and processed prior to signature generation. Theprocessing SHOULD create a new document in which relative URIs havebeen converted to absolute URIs, thereby mitigating any security riskfor the new document.
In the second case, the loss of externalunparsed entity references and the notations that bind them to applicationsmeans that canonical forms cannot properly distinguish among XML documentsthat incorporate unparsed data via this mechanism. This is an unusualcase precisely because most XML processors currently discard the documenttype declaration, which discards the notation, the entity's bindingto a URI, and the attribute type that binds the attribute value toan entity name. For documents that must be subjected to more thanone XML processor, the XML design typically indicates a referenceto unparsed data using a URI in the attribute value.
In thethird case, the loss of attribute types can affect the canonical formin different ways depending on the type. Attributes of type ID, otherthan thexml:id
attribute, cease to be ID attributes.Hence, any XPath expressions that refer to the canonical form usingtheid()
function cease to operate. The attribute typesENTITY and ENTITIES are not part of this case; they are covered inthe second case above. Attributes of enumerated type and of type ID,IDREF, IDREFS, NMTOKEN, NMTOKENS, and NOTATION fail to be appropriatelyconstrained during future attempts to change the attribute value ifthe canonical form replaces the original document during applicationprocessing. Applications can avoid the difficulties of this case byensuring that an appropriate document type declaration is prependedprior to using the canonical form in further XML processing. Thisis likely to be an easy task since attribute lists are usually acquiredfrom a standard external DTD subset, and any entity and notation declarationsnot also in the external DTD subset are typically constructed fromapplication configuration information and added to the internal DTDsubset.
While these limitations are not severe, it would bepossible to resolve them in a future version of XML canonicalizationif, for example, a new version of XPath were created based on theXML Information Set[Infoset] currently underdevelopment at the W3C.
The data model defined in the XPath 1.0 Recommendation[XPath] is used to represent the input XML documentor document subset. Implementations SHOULD but need not be based onan XPath implementation. XML canonicalization is defined in termsof the XPath definition of a node-set, and implementations MUST produceequivalent results.
The first parameter of input to the XMLcanonicalization method is either an XPath node-set or an octet streamcontaining a well-formed XML document. Implementations MUST supportthe octet stream input and SHOULD also support the document subsetfeature via node-set input. For the purpose of describing canonicalizationin terms of an XPath node-set, this section describes how an octetstream is converted to an XPath node-set.
The second parameter of input to the XML canonicalizationmethod is a boolean flag indicating whether or not comments shouldbe included in the canonical form output by the XML canonicalizationmethod. If a canonical form contains comments corresponding tothe comment nodes in the input node-set, the result is calledcanonicalXML with comments. Note that the XPath data model does not createcomment nodes for comments appearing within the document type declaration.Implementations are REQUIRED to be capable of producing canonicalXML excluding all comments that may have appeared in the input documentor document subset. Support for canonical XML with comments is RECOMMENDED.
If an XML document must be converted to a node-set, XPath REQUIRESthat an XML processor be used to create the nodes of its data modelto fully represent the document. The XML processor performs the followingtasks in order:
The input octet stream MUST contain a well-formed XML document,but the input need not be validated. However, the attribute valuenormalization and entity reference resolution MUST be performed inaccordance with the behaviors of a validating XML processor. As well,nodes for default attributes (declared in the ATTLIST with anAttValue but notspecified) are created in each element. Thus, the declarations inthe document type declaration are used to help create the canonicalform, even though the document type declaration is not retained inthe canonical form.
The XPath data model represents data usingUCS characters. Implementations MUST use XML processors that supportUTF-8 andUTF-16 and translateto the UCS character domain. For UTF-16, the leading byte order markis treated as an artifact of encoding and stripped from the UCS characterdata (subsequent zero width non-breaking spaces appearing within theUTF-16 data are not removed)[UTF-16, Section 3.2]. Support forISO-8859-1 encoding is RECOMMENDED,and all other character encodings are OPTIONAL.
All whitespacewithin the root document element MUST be preserved (except for any#xD characters deleted by line delimiter normalization). This includesall whitespace in external entities. Whitespace outside of the rootdocument element MUST be discarded.
In the XPath data model,there exist the following node types: root, element, comment, processinginstruction, text, attribute and namespace. There exists a singleroot node whose children are processing instruction nodes and commentnodes to represent information outside of the document element (andoutside of the document type declaration). The root node also hasa single element node representing the top-level document element.Each element node can have child nodes of type element, text, processinginstruction, and comment. The attributes and namespaces associatedwith an element are not considered to be child nodes of the element,but they are associated with the element by inclusion in the element'sattribute and namespace axes. Note that attribute and namespace axesmay not directly correspond to the text appearing in the element'sstart tag in the original document.
Note: An elementhas attribute nodes to represent the non-namespace attribute declarationsappearing in its start tagas well as nodes to represent thedefault attributes.
By virtue of the XPath data model, XMLcanonicalization is namespace-aware[Names]. However, it cannot and therefore does not account for namespaceequivalencies using namespace prefix rewriting (seeexplanation in Section 4). In theXPath data model, each element and attribute has a name returned bythe functionname()
which can, at the discretion of theapplication, be the QName appearing in the original document. XMLcanonicalization REQUIRES that the XML processor retain sufficientinformation such that the QName of the element as it appeared in theoriginal document can be provided.
Note: An elementE has namespace nodes that represent its namespace declarationsas well as any namespace declarations made by its ancestors thathave not been overridden inE's declarations, the defaultnamespace if it is non-empty, and the declaration of the prefixxml
.
Note: This specification supports the recentXML plenary decision to deprecate relativenamespace URIs as follows: implementations of XML canonicalizationMUST report an operation failure on documents containing relativenamespace URIs. XML canonicalization MUST NOT be implemented withan XML parser that converts relative URIs to absolute URIs.
Character content is represented in the XPath data model with textnodes. All consecutive characters are placed into a single text node.Furthermore, the text node's characters are represented in the UCScharacter domain. The XML canonicalization method does not performcharacter model normalization (seeexplanationin Section 4). However, the XML processor used to prepare theXPath data model input is REQUIRED to use Unicode Normalization FormC [NFC,NFC-Corrigendum] when converting an XML document to the UCS character domain fromany encoding that is not UCS-based (currently, UCS-based encodingsinclude UTF-8, UTF-16, UTF-16BE, and UTF-16LE, UCS-2, and UCS-4).
Since XML canonicalization converts an XPath node-set into acanonical form, the first parameter MUST either be an XPath node-setor it must be converted from an octet stream to a node-set by performingthe XML processing necessary to create the XPath nodes described above,then setting an initial XPath evaluation context of:
and evaluating the following default expression:
(//. | //@* | //namespace::*)[not(self::comment())]
(//. | //@* | //namespace::*)
The expressions in this table generate a node-set containingevery node of the XML document (except the comments if the commentparameter value is false).
If the input is an XPath node-set,then the node-set must explicitly contain every node to be renderedto the canonical form. For example, the result of the XPath expressionid("E")
is a node-set containing only the node correspondingto the element with an ID attribute value of "E". Since none of itsdescendant nodes, attribute nodes and namespace nodes are in the set,the canonical form would consist solely of the element's start andend tags, less the attribute and namespace declarations, with no internalcontent.Section 3.7 exemplifieshow to serialize an identified element along with its internal content,attributes and namespace declarations.
Although an XPath node-set is defined to be unordered, the XPath1.0 Recommendation[XPath] defines the termdocument order to be the order in which the first character ofthe XML representation of each node occurs in the XML representationof the document after expansion of general entities, except for namespaceand attribute nodes whose document order is application-dependent.
The XML canonicalization method processes a node-set by imposingthe following additional document order rules on the namespace andattribute nodes of each element:
Lexicographic comparison, which orders strings from leastto greatest alphabetically, is based on the UCS codepoint values,which is equivalent to lexicographic ordering based on UTF-8.
The XPath node-set is converted into an octet stream,the canonical form, by generating the representative UCS charactersfor each node in the node-set in ascendingdocument order, then encoding the result in UTF-8 (without aleading byte order mark). No node is processed more than once. Notethat processing an element nodeE includes the processingof all members of the node-set for whichE is an ancestor.Therefore, directly after the representative text forE is generated,E and all nodes for whichE is an ancestor are removed from the node-set (or some logicallyequivalent operation occurs such that the node-set's next node indocument order has not been processed). Note, however, that an elementnode is not removed from the node-set until after its children areprocessed.
The result of processing a node depends on its typeand on whether or not it is in the node-set. If a node is not in thenode-set, then no text is generated for the node except for the resultof processing its namespace and attribute axes (elements only) andits children (elements and the root node). If the node is in the node-set,then text is generated to represent the node in the canonical formin addition to the text generated by processing the node's namespaceand attribute axes and child nodes.
NOTE: The node-setis treated as a set of nodes, not a list of subtrees. To canonicalizean element including its namespaces, attributes, and content, thenode-set must actually contain all of the nodes corresponding to theseparts of the document, not just the element node.
The textgenerated for a node is dependent on the node type and given in thefollowing list:
xmlns=""
if and only if the following conditions are met:The latter condition eliminates unnecessary occurrences ofxmlns=""
in the canonical form since an element only receivesanxmlns=""
if its default namespace is empty and ifit has an immediate parent in the canonical form that has a non-emptydefault namespace. To finish processingL, simply processevery namespace node inL, except omit namespace nodewith local namexml
, which defines thexml
prefix, if its string value ishttp://www.w3.org/XML/1998/namespace
.
xmlns
for the text of the local name in placeof the empty local name (in XPath, the default namespace node hasan empty URI and local name).&
, all open angle brackets (<) with<
, allquotation mark characters with"
, and the whitespacecharacters #x9, #xA, and #xD, with character references. The characterreferences are written in uppercase hexadecimal with no leading zeroes(for example, #xD is represented by the character reference
).&
, all open angle brackets (<)are replaced by<
, all closing angle brackets(>) are replaced by>
, and all #xD charactersare replaced by
.<?
), the PI target name of the node, a leading spaceand the string value if it is not empty, and the closing PI symbol(?>
). If the string value is empty, then the leadingspace is not added. Also, a trailing #xA is rendered after the closingPI symbol for PI children of the root node with a lesser documentorder than the document element, and a leading #xA is rendered beforethe opening PI symbol of PI children of the root node with a greaterdocument order than the document element.<!--
), the string value of the node, and theclosing comment symbol (-->
). Also, a trailing #xA isrendered after the closing comment symbol for comment children ofthe root node with a lesser document order than the document element,and a leading #xA is rendered before the opening comment symbol ofcomment children of the root node with a greater document order thanthe document element. (Comment children of the root node representcomments outside of the top-level document element and outside ofthe document type declaration).TheQName of a node is either the local name if the namespace prefixstring is empty or the namespace prefix, a colon, then the local nameof the element. The namespace prefix used in the QName MUST be thesame one which appeared in the input document.
Some applications require the ability to create a physical representationfor an XML document subset (other than the one generated by default,which can be a proper subset of the document if the comments are omitted).Implementations of XML canonicalization that are based on XPath canprovide this functionality with little additional overhead by acceptinga node-set as input rather than an octet stream. The processing ofan element nodeE MUST be modified slightly when anXPath node-set is given as input and the element's parent is omittedfrom the node-set. This is necessary because omitted nodes SHALL notbreak the inheritance rules of inheritable attributes[C14N-Issues] defined in the xml namespace.
[Definition:]Simple inheritable attributes are attributesthat have a value that requires at most a simple redeclaration. Thisredeclaration is done by supplying a new value in the child axis.The redeclaration of a simple inheritable attributeA contained in one ofE's ancestors is done by supplyinga value to an attributeAe insideE withthe same name. Simple inheritable attributes arexml:lang
andxml:space
.
The method for processing theattribute axis of an elementE in the node-set is henceenhanced. All element nodes alongE's ancestor axisare examined for the nearest occurrences of simple inheritable attributesin the xml namespace, such asxml:lang
andxml:space
(whether or not they are in the node-set). From this list of attributes,any simple inheritable attributes that are already inE's attribute axis (whether or not they are in the node-set) are removed.Then, lexicographically merge this attribute list with the nodes ofE's attribute axis that are in the node-set. The resultof visiting the attribute axis is computed by processing the attributenodes in this merged attribute list.
Thexml:id
attribute is not a simple inheritable attribute and no processingof these attributes is performed.
Thexml:base
attribute is not a simple inheritable attribute and requires specialprocessing beyond a simple redeclaration. Hence the processing ofE's attribute axis needs to be enhanced further. A "join-URI-References"function is used forxml:base
fix up. It incorporatesxml:base
attribute values from omittedxml:base
attributes and updates thexml:base
attribute valueof the element being fixed up.
Anxml:base
fixupis performed on an elementE as follows. LetE be an element in the node set whose ancestor axis containssuccessive elementsEn ...E1 (in reversedocument order) that are omitted andE=En+1 is included. (It is important to note thatEn ...E1 is for contiguously omitted elements, for exampleonlye2 in the example in Section 3.8.) The fix-up is onlyperformed if at least one ofE1 ...En had anxml:base
attribute. In that case letX1 ...Xm be the values of thexml:base
attributes onE1 ...En+1 (in documentorder, from outermost to innermost,m <=n+1). The sequence of values is reduced in reverse document orderto a single value by first combiningXm withXm-1, then the result withXm-2, and so on by callingthe "join-URI-References" function until the new value forE'sxml:base
attribute remains. The result may alsobe null or empty (xml:base=""
) in which casexml:base
MUST NOT be rendered.
Note that thisxml:base
fixup is only performed if an element with anxml:base
attribute is removed. Specifically, it is not performed if the elementis present but the attribute is removed.
The join-URI-Referencesfunction takes anxml:base
attribute value from an omittedelement and combines it with other contiguously omitted values tocreate a value for an updatedxml:base
attribute. A simplemethod for doing this is similar to that found in sections 5.2.1,5.2.2 and 5.2.4 ofRFC 3986 with the followingmodifications:
xml:base
attribute values that includerelative path components (i.e., path components that do not beginwith a '/' character) results in an attribute value that is a relativepath component.Then, lexicographically merge this fixed up attribute withthe nodes ofE's attribute axis that are in the node-set.The result of visiting the attribute axis is computed by processingthe attribute nodes in this merged attribute list.
Attributesin the XML namespace other thanxml:base
,xml:id
,xml:lang
, andxml:space
MUST be processedas ordinary attributes.
The following examples illustratethe modification of the "Remove Dot Segments" algorithm:
"abc/"
and"../"
should result in""
"../"
and"../"
are combined as"../../"
and the result is"../../"
".."
and".."
are combined as"../../"
and the result is"../../"
To illustrate the last example, when the elementsb andc are removed from the following sample XML document,the correct result for thexml:base
attribute on elementd would be"../../x"
:
<axml:base="foo/bar">
<bxml:base="..">
<cxml:base="..">
<dxml:base="x">
</d>
</c>
</b>
</a>
The examples in this section assume a non-validating processor,primarily so that a document type declaration can be used to declareentities as well as default attributes and attributes of various types(such as ID and enumerated) without having to declare all attributesfor all elements in the document. As well, one example contains anelement that deliberately violates a validity constraint (becauseit is still well-formed).
Input Document | <?xml version="1.0"?> |
Canonical Form (uncommented) | <?xml-stylesheet href="doc.xsl" |
Canonical Form (commented) | <?xml-stylesheet href="doc.xsl" |
Demonstrates:
Input Document | <doc> |
Canonical Form | <doc> |
Demonstrates:
Note: In this example, the input document and canonicalform are identical. Both end with '>' character.
Input Document | <!DOCTYPE doc [<!ATTLIST e9 attr CDATA "default">]> |
Canonical Form | <doc> |
Demonstrates:
Note: Some start tags in the canonical form are verylong, but each start tag in this example is entirely on a single line.
Note: Ine5
,b:attr
precedesa:attr
because the primary key is namespace URI not namespaceprefix, andattr2
precedesb:attr
becausethe default namespace is not applied to unqualified attributes (sothe namespace URI forattr2
is empty).
Input Document | <!DOCTYPE doc [ |
Canonical Form | <doc> |
Demonstrates:
Note: The last element,normId
, is well-formedbut violates a validity constraint for attributes of type ID. Fortesting canonical XML implementations based on validating processors,remove the line containing this element from the input and canonicalform. In general, XML consumers should be discouraged from using thisfeature of XML.
Note: Whitespace character referencesother than   are not affected by attribute value normalization[XML].
Note: In the canonical form,the value of the attribute namedattr
in the elementnorm
begins with a space, an apostrophe (single quote), thenfour spaces before the first character reference.
Note: Theexpr
attribute of the secondcompute
element contains no line breaks.
<!DOCTYPE doc [
<!ATTLIST doc attrExtEnt ENTITY#IMPLIED>
<!ENTITY ent1 "Hello">
<!ENTITY ent2 SYSTEM"world.txt">
<!ENTITY entExt SYSTEM "earth.gif" NDATA gif>
<!NOTATION gif SYSTEM "viewgif.exe">
]>
<doc attrExtEnt="entExt">
&ent1;, &ent2;!
</doc>
<!-- Let world.txt contain "world" (excluding the quotes) -->
<doc attrExtEnt="entExt">
Hello,world!
</doc>
Demonstrates:
Input Document | <?xml version="1.0" encoding="ISO-8859-1"?> |
Canonical Form | <doc>#xC2#xA9</doc> |
Demonstrates:
Note: The content of the doc element is NOT the string#xC2#xA9 but rather the two octets whose hexadecimal values are C2and A9, which is the UTF-8 encoding of the UCS codepoint for the copyrightsign (©).
<!DOCTYPE doc [
<!ATTLIST e2 xml:space (default|preserve)'preserve'>
<!ATTLIST e3 id ID #IMPLIED>
]>
<docxmlns="http://www.ietf.org" xmlns:w3c="http://www.w3.org">
<e1>
<e2 xmlns="">
<e3id="E3"/>
</e2>
</e1>
</doc>
<!-- Evaluate with declaration xmlns:ietf="http://www.ietf.org"-->
(//. | //@* | //namespace::*)
[
self::ietf:e1or (parent::ietf:e1 and not(self::text() or self::e2))
or
count(id("E3")|ancestor-or-self::node()) = count(ancestor-or-self::node())
]
<e1 xmlns="http://www.ietf.org" xmlns:w3c="http://www.w3.org"><e3xmlns="" xml:space="preserve"></e3></e1>
Demonstrates:
xml
namespace indocument subsetsNote: In the document subset expression, the subexpression(//. | //@* | //namespace::*)
selects all nodes in the inputdocument, subjecting each to the predicate expression in square brackets.The expression is true fore1
and its implicit namespacenodes, and it is true if the element identified by E3 is in theancestor-or-self
path of the context node (such that ancestor-or-selfstays the same size under union with the element identified by E3).
Note: The canonical form contains no line delimiters.
Input Document | <!DOCTYPE doc [ |
Document Subset Expression | <!-- Evaluate with declaration xmlns:ietf="http://www.ietf.org"--> |
Canonical Form | <e1 xmlns="http://www.ietf.org" xmlns:w3c="http://www.w3.org"xml:base="something/else"><e3 xmlns="" xml:base="something/bar/foo"xml:space="preserve"></e3></e1> |
Demonstrates:
xml:id
not inherited.xml:space
)xml:base
fixup performedThis section discusses a number of key decision points as well asa rationale for each decision. Although this specification now definesXML canonicalization in terms of theXPath datamodel rather thanXML Infoset, the canonicalform described in this document is quite similar in most respectsto the canonical form described in the January 2000 Canonical XMLdraft[C14N-20000119]. However, somedifferences exist, and a number of the subsections discuss the changes.
The XML declaration, including version number and character encodingis omitted from the canonical form. The encoding is not needed sincethe canonical form is encoded in UTF-8. The version is not neededsince the absence of a version number unambiguously indicates XML1.0.
Future versions of XML will be required to include anXML declaration to indicate the version number. However, canonicalizationmethod described in this specification may not be applicable to futureversions of XML without some modifications. When canonicalizationof a new version of XML is required, this specification could be updatedto include the XML declaration as presumably the absence of the XMLdeclaration from the XPath data model can be remedied by that time(e.g. by reissuing a new XPath based on theInfoset data model).
The Unicode standard[Unicode] allows multiple different representationsof certain "precomposed characters" (a simple example is "ç").Thus two XML documents with content that is equivalent for the purposesof most applications may contain differing character sequences. TheW3C is preparing a normalized representation[CharModel]. TheC14N-20000119 CanonicalXML draft used this normalized form. However, many XML 1.0 processorsdo not perform this normalization. Furthermore, applications thatmust solve this problem typically enforce character model normalizationat all times starting when character content is created in order toavoid processing failures that could otherwise result (e.g. see examplefromCowan). Therefore, character modelnormalization has been moved out of scope for XML canonicalization.However, the XML processor used to prepare the XPath data model inputis required (by theData Model) to use NormalizationForm C [NFC,NFC-Corrigendum] when converting an XML document to the UCS character domain fromany encoding that is not UCS-based (currently, UCS-based encodingsinclude UTF-8, UTF-16, UTF-16BE, and UTF-16LE, UCS-2, and UCS-4).
TheC14N-20000119 Canonical XML draft placeda #xA after each PI outside of the document element as well as a #xAafter the end tag of the document element. The method in this specificationperforms the same function except for omitting the final #xA afterthe last PI (or comment or end tag of the document element). Thistechnique ensures that PI (and comment) children of the root are separatedfrom markup by a line feed even if root node or the document elementare omitted from the output node-set.
TheC14N-20000119 Canonical XMLdraft described a method for rewriting namespace prefixes such thattwo documents having logically equivalent namespace declarations wouldalso have identical namespace prefixes. The goal was to eliminatedependence on the particular namespace prefixes in a document whentesting for logical equivalence. However, there now exist a numberof contexts in which namespace prefixes can impart information valuein an XML document. For example, an XPath expression in an attributevalue or element content can reference a namespace prefix. Thus, rewritingthe namespace prefixes would damage such a document by changing itsmeaning (and it cannot be logically equivalent if its meaning haschanged).
More formally, let D1 be a document containing anXPath in an attribute value or element content that refers to namespaceprefixes used in D1. Further assume that the namespace prefixes inD1 will all be rewritten by the canonicalization method. Let D2 =D1, then modify the namespace prefixes in D2 and modify the XPathexpression's references to namespace prefixes such that D2 and D1remain logically equivalent. Since namespace rewriting does not includeoccurrences of namespace references in attribute values and elementcontent, the canonical form of D1 does not equal the canonical formof D2 because the XPath will be different. Thus, although namespacerewriting normalizes the namespace declarations, the goal eliminatingdependence on the particular namespace prefixes in the document isnot achieved.
Moreover, it is possible to prove that namespacerewriting is harmful, rather than simply ineffective. Let D1 be adocument containing an XPath in an attribute value or element contentthat refers to namespace prefixes used in D1. Further assume thatthe namespace prefixes in D1 will all be rewritten by the canonicalizationmethod. Now let D2 be the canonical form of D1. Clearly, the canonicalforms of D1 and D2 are equivalent (since D2 is the canonical formof the canonical form of D1), yet D1 and D2 are not logically equivalentbecause the aforementioned XPath works in D1 and doesn't work in D2.
Note that an argument similar to this can be leveled againstthe XML canonicalization method based on any of the cases in theLimitations, the problems cannot easily befixed in those cases, whereas here we have an opportunity to avoidpurposefully introducing such a limitation.
Applications thatmust test for logical equivalence must perform more sophisticatedtests than mere octet stream comparison. However, this is quite likelyto be necessary in any case in order to test for logical equivalenciesbased on application rules as well as rules from other XML-relatedrecommendations, working drafts, and future works.
TheC14N-20000119 Canonical XML draft alternated between namespace declarations andattribute declarations. This is part of the namespace prefix rewritingscheme, which this specification eliminates. This specification followsthe XPath data model of putting all namespace nodes before all attributenodes.
Unnecessary namespacedeclarations are not made in the canonical form. Whether for an emptydefault namespace, a non-empty default namespace, or a namespace prefixbinding, the XML canonicalization method omits a declaration if itdetermines that the immediate parent elementin the canonical form has an equivalent declaration in scope. The root document elementis handled specially since it has no parent element. All namespacedeclarations in it are retained, except the declaration of an emptydefault namespace is automatically omitted.
Relative to themethod of simply rendering the entire namespace context of each element,implementations are not hindered by more than a constant factor inprocessing time and memory use. The advantages include:
xmlns=""
from canonical formsof applications that may not even use namespaces, or support themonly minimally.Note that in document subsets, an element with omissionsfrom its ancestral element chain will be rendered to the canonicalform with namespace declarations that may have been made in its omittedancestors, thus preserving the meaning of the element.
e3
in the following examples is not namespace qualified, wecannot tell the difference between<e1 xmlns="a:b"><e2xmlns=""><e3/></e2></e1>
versus<e1 xmlns="a:b"><e2><e3xmlns=""/></e2></e1>
. All we know is thate3
was not namespace qualified on input, so we preserve this informationon output ife2
is omitted so thate3
doesnot take on the default namespace qualification ofe1
.The followinginformative table outlines example results of the modified RemoveDot Segments algorithm described in Section 2.4.