Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Schema that allows anything (xsd:any)

Tags:

xml

xsd

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.

like image 910
Shadowxvii Avatar asked Sep 23 '14 20:09

Shadowxvii


People also ask

Is XML Schema and XSD are same?

XML Schema Definition (XSD) language is the current standard schema language for all XML documents and data. On May 2, 2001, the World Wide Web Consortium (W3C) published XSD in its version 1.0 format. The XML Schema definition language (XSD) enables you to define the structure and data types for XML documents.

Which schema tag allows to specify elements in any order?

<xsd:all> Element Allows the elements in the group to appear (or not appear) in any order in the containing element.

Which schema is commonly known as XSD?

An XML Schema describes the structure of an XML document. The XML Schema language is also referred to as XML Schema Definition (XSD).

Can we generate XSD from XML?

The XML editor lets you create an XML Schema definition language (XSD) schema from an XML document.


2 Answers

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.

like image 110
kjhughes Avatar answered Sep 25 '22 02:09

kjhughes


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.

like image 43
Michael Kay Avatar answered Sep 25 '22 02:09

Michael Kay