RE: [xsl] Sorting the complete xml file

Subject: RE: [xsl] Sorting the complete xml file
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Sun, 11 Dec 2005 16:20:36 -0000
> What is this serializer? Is the implememtation of the 
> serializer defined by W3C? 

The term "serializer" is not actually used in the XSLT 1.0 specification,
though it is widely used in the XSLT literature, and is defined as a
technical term in the 2.0 specification.

The serializer is the component that converts a tree (typically, the result
tree produced by an XSLT transformation) into lexical XML. That is, it's the
inverse of a parser. The behavior of the serializer is described in section
16 of the XSLT 1.0 specification, and in
the 2.0 specs it's in a separate document all on its own:

> Does it guaranty
> that the order will always be the same, every time it is ran? 

No. In general, it only prescribes the serialized output in terms of
"round-tripping": when you serialize a tree, and then put the resulting
lexical XML through a parser, you get back an "equivalent" tree. The order
of attributes produced by a serializer is therefore not defined (since the
parser is allowed to deliver the results in a different order anyway). 

> My ultimate goal is to compare 2 xml
> files. So it doesn't really matter if I can't sort the attributes.

If you want to compare two lexical XML files, it's best to turn them into
Canonical XML before comparing them. This will iron out differences such as
<a href="x"/> versus <a  href='x'></a>. Alternatively, use a tool such as
DeltaXML which has already addressed these issues.

> From an XML file, what can we sort?(Node name?, Node value?, 
> etc). 

In XSLT, you can sort nodes on any value that is computable using an XPath
expression evaluated with that node as the context node. These certainly
include the name and string value of the node.

My idea is to sort from the
> left side of the tag to the right side, like sorting nodes in 
> a tree level by level until you
> reach the leaf node. 

I'm afraid I don't really understand what you mean by "the left side of a

Michael Kay

Current Thread