14

I need an example of XML schema that will allow anything and everything.

It might sound weird like this, but I need that to debug my current schema. The thing is that I have a complex object that I use in a function (part of a DLL I have no control over) along with a schema, and that functions returns me the XML. For now the function throws an exception because there's an error while validating with the schema, but there shouldn't be one. So, I want a blank schema, a schema that will not cause any validation error, so I can see the XML outputted by the function.

I tried to take my current schema, and keep only the xs:schema tag to create an empty schema, but that obviously didn't work.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Shadowxvii
  • 1,080
  • 2
  • 12
  • 31

3 Answers3

25

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.

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
3

It's often assumed that an XML schema partitions documents into those that are valid and those that are not. It's actually rather more subtle than that. When you invoke validation, you need to say how you want the validation done. The most common invocation is strict validation, in which case either the name of the root element in your instance document must correspond to the name of a global element declaration in your schema, or it must have an xsi:type attribute that matches a global type definition in your schema. It follows that no finite schema will match every document instance under strict validation.

In principle you can also invoke a schema processor to do lax validation. In this case, if there is no match for the root element name among the global element declarations in the schema, validation succeeds. So the empty schema (no declarations) matches every instance document under lax validation.

You can also invoke validation against a named type. If you invoke validation against the named type xs:anyType, then every instance is valid, regardless what the schema says.

Caveat: I've considerably simplified the rules here.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Having said all this, the practical answer to your use case is: if you want to treat your XML as valid regardless what it contains, then skip the validation step. – Michael Kay Sep 24 '14 at 07:34
  • 1
    But the thing is that I don't have any control on how the XML and the scema are used. All I can do is pass an object and a schema to a function part of an external DLL. – Shadowxvii Sep 24 '14 at 13:39
  • Then perhaps the best solution is to read the document, discover the root element name, and generate a schema which declares this root element name with type="xs:anyType". – Michael Kay Sep 24 '14 at 14:40
0

You don't need or want a schema. XML documents can be classified as "well-formed" and "valid".

As the above answer states:

XML that adheres to the XML standard is considered well formed, while XML that adheres to a DTD [or schema] is considered valid.

I should point out that all valid XML documents are also well-formed. But in your case, you don't care about the 'validity' of the XML document, just that it is well-formed. All XML parsers can (indeed must) check for well-formedness, and doing schema validation usually requires additional steps.

Look into how you can do the former (check for well-formedness) without forcing the latter (validation).

Community
  • 1
  • 1