开发者

Is the tag order relevant for valid XML?

开发者 https://www.devze.com 2023-01-23 21:22 出处:网络
in other words: <?xml version=\"1.0\" encoding=\"UTF-8\"?> <root> <tag1> <tag1.1></开发者_如何学JAVAtag1.1>

in other words:

<?xml version="1.0" encoding="UTF-8"?>
<root>
 <tag1>
  <tag1.1></开发者_如何学JAVAtag1.1>
 </tag1>
 <tag2 />
<root>

is the same as:

<?xml version="1.0" encoding="UTF-8"?>
    <root>
     <tag2 />
     <tag1>
      <tag1.1></tag1.1>
     </tag1>     
    <root>

?


In as much detail as they are shown, your two examples are semantically the same.

It's a common misunderstanding of XML that in a well-formed XML document the order of sibling elements is significant. The XML 1.0 spec specifies that attributes are unordered, however it says nothing about elements. Therefore an XML processor is free to report element siblings in any order it likes.

That said, I don't think any commonly used XML processors report elements in a different order to the order they appear in a document.

You ask about a 'valid' document – this implies that a DTD or schema is in use, and therefore it may (or may not) be the case that the order is relevant. There are mechanisms for a DTD or schema to specify that element order is relevant within a document. However, your examples don't show use of a DTD or schema.


It is not important for XML itself. Both documents are of course well-formed XML documents. However, they may not be valid; when validating with an XML Schema, it is possible to specify an order, and not providing the elements in that order will make the document invalid according to the schema. This can be done with the xsd:sequence element as described here


It depends on the DTD (or schema) of your language definition.


That depends on what is consuming the XML. The order of the elements may not be important in terms of the XML document being well formed (depending on the XSD), but certain programs / APIs expect to read the XML in alphabetical order (such as the DataContractSerializer in .NET).

Also, the order might be semantically important (e.g. if the XML is representing a document, order is very important).


XML maintains an order of documents - those are considered two different documents.

Your application may or may not consider the order of elements to be important. For example, the order of elements is essential in XHTML. If you are validating against a Schema or similar, it depends on the schema whether or not both documents are valid.

Note that the order of attributes is not important however (XML documents that differ only in the order of attributes on elements are considered identical).

0

精彩评论

暂无评论...
验证码 换一张
取 消