Alex Rivera | Logout

xml schema and using a choice as the document root

Asked 2009-10-28T18:38:43.207
16

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

Edit
Report

1 Answer

1

I came across this post and I thought it is worth mentioning what I figure from Spring Web Services world (those that give importance to data contract first).

One of the good ways of negotiating this root element problem is to directly define multiple root elements under the schema element like Phil Booth has mentioned.

However when it comes to best practices and web services framework that give priority to data first a fool proof schema design is important. When someone defines a schema like this -

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

a web service framework like Spring-WS, which relies on this so called data contract schema to generate the web service, may not be able to understand if <foo> or <bar> is the request root element to the service.

Please refer to this Link - Data Contract

In such cases, I have found the approach given by "John at CashCommons" or Stephen Rushing useful.

answered 2010-03-29T14:42:04.797

Your Answer