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: 191
I need to create a schema for an XML file that is pretty flexible. It has to meet the following requirements:
- Validate some elements that we require to be present, and know the exact structure of
- Validate some elements that are optional, and we know the exact structure of
- Allow any other elements
- 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