views:

72

answers:

1

I want to write an xsd for the xmlrpc spec (and generate java classes out of it using jaxb). The xmlrpc spec allows values like:

<value><int>123</int></value>
<value><boolean>1</boolean></value>

But at the same time it requires:

If no type is indicated, the type is string.

Which means i could receive something like this:

<value>test123</value>

which is equivalent to

<value><string>test123</string></value>

Is there a way to define this in an xsd.

+1  A: 

Yes, set a mixed content model on value:

<xs:complexType name="valuetype" mixed="true">
  <xs:sequence>
    <xs:element name="int" type="xs:int"/>
    <xs:element name="boolean" type="xs:boolean"/>
    ...
  </xs:sequence>
</xs:complexType>
xcut
I believe that does not forbid `<value>1<boolean>1</boolean></value>`
Tomalak
@Tomalak: Correct. Mixed is not restrictive enough.
Willi
And mixed="true" generates a List<Serializable> in java instead of strongly typed fields with accessor methods.
Willi
Tomalak is right that this will be allowed, but you still do not have a choice about this. The base of your type will have to be either a simple type (i.e. a string, integer, etc) or a complex type. And if it is a complex type, the only way to allow text is to set it to mixed. You can't have a type that sometimes is a string and sometimes a complex type.
xcut
As for the "list in Java", I will assume you are using JAXB; then the list will be a mixture of String objects and your objects, you will have to use instanceof in your traversal.
xcut
I **hate** instanceof checks so this is no option for me. And having both text and elements inside a value is forbidden by the xmlrpc spec.
Willi
Well, listen, I answered your question, I can't help you any further. This is as far as you will get with XML Schema. If it is not satisfactory to you, you will have to switch to RelaxNG (for example): http://www.relaxng.org/
xcut
Is it possible using relaxng?
Willi
Yes, because in RelaxNG you get a "<text/>" command that you can interleave with elements in any way you want, including have a choice between text and elements. Here is an interesting overview: http://books.xmlschemata.org/relaxng/relax-CHP-6-SECT-6.html
xcut
It's sad but Jaxb2 does not seem to support RelaxNG. It fails with ` datatype library "http://www.w3.org/2001/XMLSchema-datatypes" not recognized`.
Willi
Good idea though.
Willi