Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate XML against XSD and ignore order of child elements

I have a method in a C# app that validates a user input XML file against an embedded XSD. It works just fine, but it requires that all the child elements be in the exact order defined in the XSD. To me though, the order doesn't matter so long as the elements exist.

For example, if I had the following XSD...

<xs:element maxOccurs="unbounded" name="ParentElement">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="FirstChildElement" type="xs:string" />
      <xs:element name="SecondChildElement" type="xs:string" />
      <xs:element name="ThirdChildElement" type="xs:string" />
    </xs:sequence>
  </xs:complexType>
</xs:element>

And an XML like this...

<ParentElement>
    <FirstChildElement>someValue</FirstChildElement>        
    <ThirdChildElement>someValue</ThirdChildElement>
    <SecondChildElement>someValue</SecondChildElement>
</ParentElement>

If I validated it I'd get an error because the child elements are out of order.

Can I make some change to the XSD so validation only cares if the elements exist, and that they're under the correct parent, but not what order they're in?

like image 268
Jim Avatar asked Jul 23 '12 21:07

Jim


People also ask

Does order of elements matter in XML?

According to the XML specification, the order of attribute specifications in a start-tag or empty-element tag is not significant.

Can we validate XML documents against a schema?

You can validate your XML documents against XML schemas only; validation against DTDs is not supported. However, although you cannot validate against DTDs, you can insert documents that contain a DOCTYPE or that refer to DTDs.

How do you validate against XSD?

Simply go to the XML Tools > Validate Now option and click on it. You can also press Ctrl + Alt + Shift + M key combination to open Validate Now option. Now, select the XSD file against which you want to validate the opened XML document. Simply browse and then import the XSD file in the respective field.


1 Answers

Sequence means, the elements must appear in the specific order. You probably want xs:all. Take a look at http://www.w3schools.com/xml/schema_complex_indicators.asp

like image 157
user1527329 Avatar answered Oct 14 '22 21:10

user1527329