Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xml schema and using a choice as the document root

Tags:

xml

xsd

I have a bit of a newbie xml schema question. I believe the answer is that what I need to do is not possible with schema, but I'd like to be sure. The problem is that I have a webservice that returns a response with one type of root element on success (say <Response>), and on a complete failure, returns a document with a different root element (say, <Exception>). So, basically, two completely different documents:

<Response>......</Response> OR
<Exception>....</Exception>

Is it possible to describe these two different documents with one schema document? It's like I want a choice as the first element under the schema element -- but that isn't valid syntax. I've tried a couple of variants that parse as valid xsd, but don't validate the documents. Any suggestions? Or is this simply not possible? Thanks very much in advance -- m

like image 870
mikey Avatar asked Oct 28 '09 18:10

mikey


People also ask

How do you define a root element in XML Schema?

The root element defines the first element of the XML document and defines a reference to the root complexType. The root complexType is the complexType from which all other complexTypes are related to in their ancestry, whether the root is the parent, grand parent, great grand parent, etc.

What is choice in XML Schema?

<xsd:choice> ElementAllows one and only one of the elements contained in the selected group to be present within the containing element.

What does root mean in XML?

The XML root element represents the XML document that is being mapped. The XML root element is a looping structure that contains elements and content particles that repeat in sequence until either the group data ends or the maximum number of times that the loop is permitted to repeat is exhausted.

What is the purpose of XML document schema?

The purpose of an XML Schema is to define the legal building blocks of an XML document: the elements and attributes that can appear in a document. the number of (and order of) child elements. data types for elements and attributes.


1 Answers

Actually, XML schema does allow you to define alternative root elements in a single schema, although not by using the choice element. Instead, all you need to do is list each of the possible roots as direct children of your schema element.

For example, given the following XML schema:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="foo">
        ...
    </xs:element>
    <xs:element name="bar">
        ...
    </xs:element>
</xs:schema>

Either of the following documents would validate against it:

<foo>
    ...
</foo>

Or:

<bar>
    ...
</bar>
like image 188
Phil Booth Avatar answered Oct 10 '22 09:10

Phil Booth