简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM