rplantiko
rplantiko

Reputation: 2738

XML Schema for mixing generic and specific content

Is it possible to express as XML schema the following class of XML documents:

Example instances:

<root>
  <message type="S">Order saved</message>
  <order number="4711"/>
</root>


<root>
  <message type="S">3 countries selected</message>
  <country value="ES">Spain</country>
  <country value="FR">France</country>
  <country value="IT">Italy</country>
</root>

My idea was to use a sequence of one xs:element and xs:any, like so:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="message" maxOccurs="1">
           <xs:complexType>
             <xs:simpleContent>
               <xs:extension base="xs:string">
                 <xs:attribute name="type"></xs:attribute>
               </xs:extension>
             </xs:simpleContent>
           </xs:complexType>
        </xs:element>
        <xs:any processContents="skip" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

But then, a document containing two "message" children would pass (since the second "message" element will comply with xs:any).

Upvotes: 1

Views: 432

Answers (1)

Michael Kay
Michael Kay

Reputation: 163282

You can constrain the element names that will match xs:any. In XSD 1.0 you can say which namespaces are accepted, in XSD 1.1 you can be more specific: in fact there's an explicit keyword notQName="##definedSibling" that does exactly what you are asking for. XSD 1.1 is currently supported in Saxon and Xerces.

Upvotes: 1

Related Questions