tags:

views:

48

answers:

1

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"&gt;
  <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?

+5  A: 

Your requirement number 3 cannot be addressed if the name elements are optional because your schema would then violate the unique particle attribution rule (basically, the processor won't know whether to validate firstname against firstname or against any).

You are not restricted to a single schema as far as validation is concerned. If you are ok with using two schemas in different namespaces, you can do this:

Schema one - allow anything:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <xs:element name="document">
    <xs:complexType>
      <xs:sequence>
        <xs:any processContents="lax" minOccurs="0" maxOccurs="unbounded" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Schema two - add specific validation for some elements:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://other" xmlns="http://other"&gt;
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
</xs:schema>

Then make sure your instance document's xsi:include references both schemas.

xcut