Does XML care about the order of elements?

后端 未结 7 1659
盖世英雄少女心
盖世英雄少女心 2020-11-30 10:55

XML confuses me sometimes, but I\'m trying to get this figured out. What the vendor is telling me doesn\'t make sense but again, XML and I don\'t get along :)

I have

相关标签:
7条回答
  • 2020-11-30 11:13

    If there is no XSD (XML schema) at hand, then all you can check for is whether or not your XML is well formed.

    In your case - it is. There are no overlapping XML tags, no XML tag that are left open or anything of that sort.

    If the vendor needs to enforce things like order inside the XML, he ought to provide an XSD file - otherwise, his "requirements" cannot be validated and checked....

    0 讨论(0)
  • 2020-11-30 11:13

    XML schema compositor "sequence" will enforce ordering

    I know this is old but I just came upon the post.

    Until today I would most likely answer the question Does XML care about the order of elements? with No, unless you use a poorly written xml parser.

    However, today a third party application complained that the xml files I created were invalid. They use an XSD file to validate the xml. And yes, you can enforce the order or elements within an xsd file:

    <xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:complexType name="ComplexType">
        <xs:sequence>
          <xs:element minOccurs="0" maxOccurs="1" default="" name="Value1" type="xs:string" />
          <xs:element minOccurs="0" maxOccurs="1" default="" name="Value2" type="xs:string" />
        </xs:sequence>
      </xs:complexType>
    </xs:schema>
    

    The keyword is xs:sequence

    The sequence element specifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times.

    which is in contrast to xs:all which does not care about the order but only allows elements which occur zero or once.

    Specifies that the child elements can appear in any order. Each child element can occur 0 or 1 time

    (The words sequence and all are both what is called a Compositor in the XML Schema definition.)

    0 讨论(0)
  • 2020-11-30 11:16

    Vendors will do what they will do, but it is a nonstandard application of XML to rely on ordering.

    XML is declarative, not procedural. So, it shouldn't be "stepwise".

    0 讨论(0)
  • 2020-11-30 11:27

    Accessing XML by DOM preserves the order of nodes the way they are in your XML document. Look here:

    http://www.w3schools.com/dom/dom_nodes_nodelist.asp

    there you find:

    A node list object represents a list of nodes, in the same order as in the XML.

    If your web service is relying on the order is a different question - it may or may not, that depends on the web service implementation.

    0 讨论(0)
  • 2020-11-30 11:27

    Well I think the best possible answer is - ask them. They could even be parsing your XML as a text file and so you might need to have line breaks and some "correct" order of attributes.

    If they would parse this correctly order wouldn't matter (at least not in terms of valid requests). As I see it they should build two tables and join them with supplied ids.

    0 讨论(0)
  • 2020-11-30 11:28

    An XML schema may or may not enforce element order. It depends on the schema in question. In the most general sense, XML element order does not matter, unless otherwise specified by the appropriate schema.

    0 讨论(0)
提交回复
热议问题