2

This is my xml model:

<train xmlns="http://www.example.org/train/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <routes>
        <route>Route1</route>
        <route>Route2</route>
    </routes>
</train>

I would like to create an XSD that will give me the following java:

Train train = new Train(); 
train.getRoutes().add(new Route());

I have tried different designs, ie Venetian Blind, Russian Doll, Salami Slice, but the end result is always java like this:

Train train = new Train(); 
train.getRoutes().getRoute().add("Route1");

Here are the xsd docs I have tried so far:

Venetian Blind

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.example.org/train/">
  <xs:element xmlns:tra="http://www.example.org/train/" name="train" type="tra:trainType"/>
  <xs:complexType name="routesType">
    <xs:sequence>
      <xs:element type="xs:string" name="route" maxOccurs="unbounded" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="trainType">
    <xs:sequence>
      <xs:element xmlns:tra="http://www.example.org/train/" type="tra:routesType" name="routes"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

Russian Doll

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.example.org/train/">
  <xs:element name="train">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="routes">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="route" maxOccurs="unbounded" minOccurs="0"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Salami Slice

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.example.org/train/">
  <xs:element name="route" type="xs:string"/>
  <xs:element name="routes">
    <xs:complexType>
      <xs:sequence>
        <xs:element xmlns:tra="http://www.example.org/train/" ref="tra:route" maxOccurs="unbounded" minOccurs="0"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="train">
    <xs:complexType>
      <xs:sequence>
        <xs:element xmlns:tra="http://www.example.org/train/" ref="tra:routes"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Can anyone tell me what I am doing wrong?

Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
dasPing
  • 337
  • 1
  • 2
  • 19

1 Answers1

2

You most probably need @XmlElementWrapper to have something like

@XmlElementWrapper(name="routes")
@XmlElement(name="route")
List<Route> routes ...;

You can use a jaxb-xew-plugin for this purpose.

See this answer:

How generate XMLElementWrapper annotation with xjc and customized binding

Community
  • 1
  • 1
lexicore
  • 42,748
  • 17
  • 132
  • 221
  • The element wrappers might work if I was going from Java to XSD, but I am going from XSD to java. – dasPing Nov 28 '14 at 13:51
  • 1
    Please reread my answer carefully. The jaxb-xew-plugin is a code generation plugin exactly for the XSD -> Java case which you seem to have. – lexicore Nov 28 '14 at 13:56
  • You are right, the plugin looks like it might be a solution. Interesting. – dasPing Nov 28 '14 at 14:02