You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by "Watson Gary (Mr GA)" <Ga...@dti.gsi.gov.uk> on 2005/11/11 18:08:44 UTC

FW: Attribute Order - alphabetical ?

> Hi
> 
> I've just changed our application to switch to Xerces 2 and noticed
> the order of attributes, while irrelevant according to the XML spec, is
> changed to alphabetical when Xerces loads up a DOM.
> 
> While I think it is fine for Xerces to sort attributes alphabetically, for
> fast attribute access times, it does have one or two side
> effects. For example, if you load up an XML document in DOM and
> simply write it back out, a "diff" of the input and output files will
> show the file to have changed because the attributes have
> been re-ordered.
> 
> As a result, I have modified the org.apache.xerces.dom.NamedNodeMapImpl
> class (attached) to allow for a new "feature" to maintain the order of
> occurrence of
> attributes as they are set on each element. This is only a minor code
> change and probably only slightly impacts performance.
> 
> Would the apache Xerces team be interested in this fix and add it in
> to the source code base?
> 
> 
> Thanks in advance,
> 
> Gary Watson
> 
> ________________________________________ 
> IT Consultant
> Department of Trade and Industry
> Energy Resource Development Unit (ERDU)
> tel: +44 (0) 20 7215 5157 <<NamedNodeMapImpl.java>> 
> 


The original of this email was scanned for viruses by the Government Secure Intranet (GSi) virus scanning service supplied exclusively by Energis in partnership with MessageLabs.

On leaving the GSi this email was certified virus-free

Re: Attribute Order - alphabetical ?

Posted by Joseph Kesselman <ke...@us.ibm.com>.
Attribute order is explicitly Not Meaningful in XML, so XML processing code
can present attributes in whatever order is most efficient for it. Xerces
uses alphabetical because, when I rattled off that code many years ago,
part of my concern was minimal storage space (so I didn't want to use a
separate hashtable) and the other part was reasonably fast access (so I
wanted something I could search reasonably rapidly).

There's no strong reason for alphabetical order -- but the two points *are*
stong reasons for not promising any paticular order, and in particular not
promising that the original order in the source document will be retained.

If you concern is being able to compare files, there are good XML-specific
diff tools available; use one of those rather than a text-based diff. Or,
as noted, canonicalize before diffing the text files.

______________________________________
Joe Kesselman, IBM Next-Generation Web Technologies: XML, XSL and more.
"The world changed profoundly and unpredictably the day Tim Berners Lee
got bitten by a radioactive spider." -- Rafe Culpin, in r.m.filk


---------------------------------------------------------------------
To unsubscribe, e-mail: j-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-dev-help@xerces.apache.org


Re: Attribute Order - alphabetical ?

Posted by Curt Arnold <ca...@apache.org>.
On Nov 11, 2005, at 11:08 AM, Watson Gary (Mr GA) wrote:
> While I think it is fine for Xerces to sort attributes  
> alphabetically, for
> fast attribute access times, it does have one or two side
> effects. For example, if you load up an XML document in DOM and
> simply write it back out, a "diff" of the input and output files will
> show the file to have changed because the attributes have
> been re-ordered


However, attribute order is only one of syntax difference that can  
get lost when an stream is parsed:  Line-feeds are normalized, white- 
space within tags are dropped, whether matching quotes or apostrophes  
are used in to enclose attribute values, and others.  If you were  
going to be able to diff an arbitrary XML file after parsing and  
serialization, you'd need to preserve a lot more than just attribute  
order or be lucky, for example, that your serializer used the same  
character to enclose attribute values.

If you are interested in the issue: take a look at the Canonical XML  
recommendation (http://www.w3.org/TR/2001/REC-xml-c14n-20010315)  
which describes a method for serializing an XML document so that  
logically equivalent documents result in a consistent stream  
representation.

p.s.: IANAXC (I am not a Xerces committer).