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

[英]Eclipse XML formatting changes order of tags?


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

我刚注意到一些奇怪的东西。如果我有这个XML:

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

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

我按下CTRL-SHIFT-F(格式)代码突然改变为:

<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 个解决方案

#1


4  

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

XML规范明确表示属性的顺序没有保证:

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

注意,start标记或empty-element标记中的属性规范的顺序并不重要。

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.

您可能会找到一种方法来更改Eclipse中XML格式化程序的行为,但是最健壮的解决方案是按名称而不是索引访问属性。否则,根据XML规范,您的代码将对完全合法的更改保持敏感。

See also:

参见:

#2


1  

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.

话虽如此,一个人拥有特定顺序的属性可能有其风格上的原因。在某些情况下,它还可以使XML文档更易于读取,以便对相关属性进行分组。

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.

从您的示例中,看起来行为可能是按字母顺序对属性进行排序,但是我还没有进行测试来确认这个理论。它还可以做的另一件事是对属性进行排序,以便它们出现在XSD/DTD中。你可能想要测试这个理论,因为它可以给你一些对顺序的控制。

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

您可能还想要打开一个增强请求,以便对该格式化程序行为进行控制。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2012/03/27/6f80e8ac1d8eb6d04619a3d03e32b5ba.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告