Creating a 'flexible' XML schema

Posted by Fiona Holder on Stack Overflow See other posts from Stack Overflow or by Fiona Holder
Published on 2010-04-07T12:03:23Z Indexed on 2010/04/07 12:13 UTC
Read the original article Hit count: 173

Filed under:
|
|

I need to create a schema for an XML file that is pretty flexible. It has to meet the following requirements:

  1. Validate some elements that we require to be present, and know the exact structure of
  2. Validate some elements that are optional, and we know the exact structure of
  3. Allow any other elements
  4. Allow them in any order

Quick example:

XML

<person>
    <age></age>
    <lastname></lastname>
    <height></height>
</person>

My attempt at an XSD:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="person">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="firstname" minOccurs="0" type="xs:string"/>
        <xs:element name="lastname" type="xs:string"/>
        <xs:any processContents="lax" minOccurs="0" maxOccurs="unbounded" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Now my XSD satisfies requirements 1 and 3. It is not a valid schema however, if both firstname and lastname were optional, so it doesn't satisfy requirement 2, and the order is fixed, which fails requirement 4.

Now all I need is something to validate my XML. I'm open to suggestions on any way of doing this, either programmatically in .NET 3.5, another type of schema etc.

Can anyone think of a solution to satisfy all 4 requirements?

© Stack Overflow or respective owner

Related posts about Xml

Related posts about xsd