XML Schema that allows anything (xsd:any)

XML Schema cannot specify that a document is valid regardless of its content.

However, if you’re able to specify the root element, you can use xs:anyAttribute and xs:any to allow any attributes on the root element and any XML under the root:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:any processContents="skip" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:anyAttribute processContents="skip"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

In your case, as long as you can be assured of a finite number of possible root element names, you can use this technique to allow any XML content under a root element with a known name.


Update: This can be written much more concisely [Credit: C. M. Sperberg-McQueen]:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root"/>
</xs:schema>

Note that this is allowing, but not requiring, root to be empty.

Leave a Comment