tags:

views:

56

answers:

2

Hi, i need to write a schema that all xml instances are valid to it. i tried:

      <xs:element name="Arguments">
    <xs:complexType>
      <xs:sequence>
         <xs:any namespace="##any" minOccurs="0" maxOccurs="unbounded" processContents="lax" />
      </xs:sequence>
    </xs:complexType>

but it enforces a root element named Arguments. is there a way for the root to be Any ?

A: 

Good question, although I am not sure it can be done. Your approach with using xs:any is good but I'm not sure it can be applied for an entire XML (i.e. the root) but just for a section of it.

To quote from a book I once read (something that puts a purpose on the "why?" people are asking): [...] useful when writing schemas for languages such as XSLT that routinely include markup from multiple vocabularies that are unknown when the schema is written [...] useful when you're just beginning to design the document structure and you don't yet have a clear picture of how everything fits together [...] (XML in a nutshell)

I'm also curious to see if it can be done or what is the best workaround for this.

dpb
A: 

The whole purpose of a schema is to constrain the potential space of valid documents. Either you're doing a whole document or you're doing a fragment. If you're doing a whole document, the correct approach is to just omit the schema entirely. Really. You've got no constraints at all (other than well-formedness) and so can't apply any interpretation to the document beyond it being an XML document.

The case where you've got a fragment is much more useful though. The best way of doing that is to have an outer element (whose name you control) that contains the uncontrolled fragment. When you do that, you need to say that the content is a sequence of zero-to-unbounded numbers of arbitrary elements, which is what you've done already. If it's really anything, you might also consider allowing mixed content (don't do that if you don't need it, of course, but if you're willing to handle things like XHTML in-paragraph content then that's what you require) and allowing arbitrary attributes on the containing element (see <xsd:anyAttribute>). It's also often a good idea to specify that there are constraints on what namespace the arbitrary elements can come from (##other being the most useful since it stops uncontrolled recursion in your schema).

So, apart from reviewing whether you've got the details right, you're probably best not trying to handle absolutely anything. Just make sure that your container element is defined right for your actual purpose.

Donal Fellows