views:

2159

answers:

3

I have following schema

<complexType name="BookShelf">
   <sequence>
      <element name="newBook" type="string" minOccurs="0" maxOccurs="unbounded"/>
      <element name="oldBook" type="string" minOccurs="0" maxOccurs="unbounded"/>
   </sequence>
</complexType>

XJC generates BookShelf class with two lists, one for newBook and one for oldBook. Excellent!

Now I want books to appear in any order. So I rewrite my schema to:

<complexType name="BookShelf">
   <sequence>
      <choice minOccurs="0" maxOccurs="unbounded">
         <element name="newBook" type="string"/>
         <element name="oldBook" type="string"/>
      </choice>
   </sequence>
</complexType>

But now XJC generates BookShelf with only one list newBookOrOldBook of type List<JAXBElement<String>>.

I don't care about the order in which books appear and I want to allow XML writer to specify books in any order he\she wishes, but I still want books of each type as List in generated BookShelf class. Is there any way I can achieve this?

A: 

Maybe something like this?

<schema 
   elementFormDefault = "qualified" 
   xmlns              = "http://www.w3.org/2001/XMLSchema"
   xmlns:xs           = "http://www.w3.org/2001/XMLSchema"
   xmlns:tns          = "urn:cheeso.examples.2009.05.listofbooks" 
   targetNamespace    = "urn:cheeso.examples.2009.05.listofbooks" 
  >

  <element name="Shelf" nillable="true" type="tns:BookShelf" />

  <complexType name="BookShelf">
    <sequence>
      <element minOccurs="0" maxOccurs="1" name="Store" type="tns:ArrayOfChoice1" />
    </sequence>
  </complexType>

  <complexType name="ArrayOfChoice1">
    <choice minOccurs="0" maxOccurs="unbounded">
      <element minOccurs="1" maxOccurs="1" name="newBook" nillable="true" type="tns:newBook" />
      <element minOccurs="1" maxOccurs="1" name="oldBook" nillable="true" type="tns:oldBook" />
    </choice>
  </complexType>

  <complexType name="Book">
    <attribute name="name" type="string" />
  </complexType>

  <complexType name="newBook">
    <complexContent mixed="false">
      <extension base="tns:Book" />
    </complexContent>
  </complexType>

  <complexType name="oldBook">
    <complexContent mixed="false">
      <extension base="tns:Book" />
    </complexContent>
  </complexType>

</schema>

Of course you could simplify to

<schema 
   elementFormDefault = "qualified" 
   xmlns              = "http://www.w3.org/2001/XMLSchema"
   xmlns:xs           = "http://www.w3.org/2001/XMLSchema"
   xmlns:tns          = "urn:cheeso.examples.2009.05.listofbooks" 
   targetNamespace    = "urn:cheeso.examples.2009.05.listofbooks" 
  >

  <element name="Shelf" nillable="true" type="tns:BookShelf" />

  <complexType name="BookShelf">
    <sequence>
      <element minOccurs="0" maxOccurs="1" name="Store" type="tns:ArrayOfChoice1" />
    </sequence>
  </complexType>

  <complexType name="ArrayOfChoice1">
    <choice minOccurs="0" maxOccurs="unbounded">
      <element minOccurs="1" maxOccurs="1" name="newBook" nillable="true" type="xs:string" />
      <element minOccurs="1" maxOccurs="1" name="oldBook" nillable="true" type="xs:string" />
    </choice>
  </complexType>

</schema>
Cheeso
Thank you for your answer. Sorry, but I was unclear in my question. Both oldBook and newBook will contain common fields as well as different and I want to avoid casting in Java code. In schema described above, when oldBook and newBook has the string type, JAXB will generate List of JAXBElement<String> and instead of instanceof and casting one has to call getName on JAXBElement to determine required type of element. May be this situation is wrong for XML design? And I should separate oldBook and newBook enumeration?
syntetic
You may have an overconstrained problem, I suspect. I don't know so much about JAXB and the possibility to shape the object model generated from the XSD. Distinguishing data of the same type by element name alone, requires your program to call getName(). If you change the shape of the schema, you could use <book name="bookname" type="new" />, in which case you would not have to call getName(). Instead you would have a different object model. Or you could specify new complexTypes for newBook and oldBook, as in my first example schema. It's up to you.
Cheeso
A: 

I don't think this is possible in JAXB, without writing some custom java or XSLT.

JAXB isn't very good at mapping between objects and xml that have different structure, like yours. Also, the ordering of the old book with respect to new books in XML would be lost when converted to two separate lists in Java, and JAXB generally wants to preserve information.

The following does not answer your question, but maybe it's a step towards what you want:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <xs:element name="bookShelf" type="BookShelf"/>
  <xs:complexType name="BookShelf">
    <xs:sequence>
      <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:element name="newBook" minOccurs="0" type="xs:string"/>
        <xs:element name="oldBook" minOccurs="0" type="xs:string"/>
      </xs:sequence>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

I haven't tried this with JAXB, but I think it will generate a list of a class with two fields, newBook and oldBook. So, you don't have to cast or use instanceof, but can just check for null to see which one it is. As I said, it's not a solution, but maybe a little bit closer.

13ren
A: 

I think I have to refuse the idea of mixing lists of different elements in one element (mixing old and new books on a bookself), especially because I'm planning to reference lists of those elements (new and old books lists) in other elements. If I don't it quickly became a nightmare in java code. I ended with the following schema:

<schema
   xmlns="http://www.w3.org/2001/XMLSchema"
   xmlns:tns="http://www.example.org/books"
   targetNamespace="http://www.example.org/books"
   elementFormDefault="qualified"
>
   <complexType name="BookShelf">
      <sequence>
         <element name="newBooks" type="tns:NewBookList" minOccurs="0" />
         <element name="oldBooks" type="tns:OldBookList" minOccurs="0" />
      </sequence>
   </complexType>

   <complexType name="NewBookList">
      <sequence>
         <element name="newBook" type="tns:NewBook" maxOccurs="unbounded" />
      </sequence>
   </complexType>

   <complexType name="OldBookList">
      <sequence>
         <element name="oldBook" type="tns:OldBook" maxOccurs="unbounded" />
      </sequence>
   </complexType>

   <complexType name="NewBook" />
   <complexType name="OldBook" />
</schema>

Thanks everyone for helping me realize this. This schema will lead to more clear and simple Java code as well as to more readable and predictable XML document.

syntetic