Eclipse XML格式改变标记的顺序?

[英]Eclipse XML formatting changes order of tags?

I just noticed something strange. If I have this XML:


<level number="7" background="background_5">

and I press CTRL-SHIFT-F (Format) the code suddenly changes to this:


<level background="background_5" number="7" >

I can hardly imagine that the changing of the file is done on purpose, since I got some serious problems while parsing the files, since I access the attributes by index.


Is there a reason, except that it's being ordered by alphabet, that it's being formatted like this, and more importantly, how do I turn it off?


2 个解决方案



The XML spec explicitly says that the order of attributes is not guaranteed:


Note that the order of attribute specifications in a start-tag or empty-element tag is not significant.


You might find a way to change the behavior of the XML formatter in Eclipse, but the most robust solution is to access attributes by name instead of index. Otherwise your code will remain sensitive to changes that are perfectly legal according to the XML spec.


See also:




I agree with Eli Acherkan's answer regarding not relying on order of attributes when processing XML documents.

我同意Eli Acherkan关于在处理XML文档时不依赖属性顺序的回答。

Having said that, one could have stylistic reasons for having attributes in a particular order. In some cases, it can also make the XML document easier to read to group related attributes.


Looking at Eclipse XML formatting preferences, I don't see anything related to attribute order.

查看Eclipse XML格式首选项,我看不到任何与属性顺序相关的内容。

Eclipse XML Formatting Preferences

From your example, it looks like the behavior might be to sort attributes alphabetically, but I haven't performed tests to confirm that theory. Another thing it could be doing is sorting attributes in order that they appear in the XSD/DTD. You may want to test this theory as that could give you some control over the order.


You may also want to open an enhancement request for a preference to control this formatter behavior.




© 2014-2019 粤ICP备14056181号