59

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 some XML that I'm sending to a vendor's web service that is giving me random failures:

<root>
    <Request>
        <Driver id="1" VehId="1">...</Driver>
        <Driver id="2" VehId="1">...</Driver>
        <Driver id="3" VehId="2">...</Driver>
        <Vehicle id="1">...</Vehicle>
        <Vehicle id="2">...</Vehicle>
        <Driver id="4" VehId="2">...</Driver>
    </Request>
</root>

There is no XSLT or XSD to compare against to see if my XML is valid.

The vendor states that the XML is invalid because Driver #4 is in the wrong area. The XPath for Driver should be root/Request/Driver and Vehicle is root/Request/Vehicle.

Is it common that XML parsers would force an element order, especially if there is no XSD to compare the XML to? The vendor's support is slow to get back with me, so I want to know what good common practice is.

Followup

I complained enough to our account rep about not being able to test this (and made it sound like they were just trying to get support money) that it turns out that the Developers have the XSD, but Support doesn't. So I had been talking to the wrong group *facepalm*

I got the XSD, and it does enforce a specific order of elements.

Now to fight them in regards to their own sample XML doesn't follow the schema, but at least now I have something to test against.

dragonmantank
  • 15,243
  • 20
  • 84
  • 92
  • 2
    The vendor should supply an XSD or supporting documentation that details assumptions the software makes about the XML format, including element order. Otherwise you have to guess. Software that reads XML documents should not enforce an order (when possible), but it is not uncommon. An XSD *is* documentation that explicitly defines the interchange format rules. See also http://kettle.pentaho.com/. – Dave Jarvis Dec 01 '10 at 20:56

7 Answers7

72

#XML schema compositor "sequence" will enforce ordering

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.)

isherwood
  • 58,414
  • 16
  • 114
  • 157
Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
  • 2
    +1 This cost me days with an Amazon AWS feed in case any one else gets stuck here. It never even crossed my mind that child element order would matter. As answer points out "sequence" is the key – jtubre Mar 12 '18 at 14:00
26

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....

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    That's exactly what I thought and I wanted to be sure. The XML parser we built works on the above XML just fine and doesn't generate any errors, but without an XSD how can you really be sure? – dragonmantank Dec 01 '10 at 21:09
  • 1
    A DTD (if the vendor is using an ancient toolset) can also determine validity. – brianary Jan 09 '13 at 21:43
  • Although there's no implied ordering without a schema to specify it, the case of mixed content may be different. Although I haven't checked the specification, if sub-elements are part of a mixed-content value then their order must be significant, or else it would be the most useless possible interpretation. – ACProctor Aug 25 '21 at 16:12
5

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".

Buggieboy
  • 4,636
  • 4
  • 55
  • 79
5

XML schemas can enforce element order. If there's no schema, neither order nor tags nor general structure not the kind of text (if it's a number or anything) is prescribed in any way - in theory. Of course that's not the case here.

It is perfectly possible to parse* data from XML without caring about order, but it can be easier (e.g. when using SAX, I imagine, or when you're a lazy bastard writing very sloppy code) to parse if you assume a certain order. Although they should include some schema if they want a certain order, it's perfectly possible that their parser chokes on it anyway. Yes, they shouldn't do this, but obviously they don't care.

*By "parsing" I don't mean "take that XML document and turn it into e.g. a DOM", but "take that e.g. DOM and extract the information from it".

4

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.

Doc Brown
  • 19,739
  • 7
  • 52
  • 88
3

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.

Reinderien
  • 11,755
  • 5
  • 49
  • 77
0

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.

Nux
  • 9,276
  • 5
  • 59
  • 72