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
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.
<xsd:choice> ElementAllows one and only one of the elements contained in the selected group to be present within the containing element.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With