LINEMAN78
LINEMAN78

Reputation: 2562

XJC xs:choice inheritance

I am having a problem getting xjc to generate a class with choice group extension that maintains order. The following schema is an example.

<xs:complexType name="base">
  <xs:choice maxOccurs="unbounded">
    <xs:element name="a" />
    <xs:element name="b" />
  </xs:choice>
</xs:complexType>

<xs:complexType name="extended">
  <xs:complexContent>
    <xs:extension base="base">
      <xs:choice>
        <xs:element name="c" />
      </xs:choice>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

The problem is that this results in both base and extended classes to contain a list property instead of extended just adding to the base list property so that order can be maintained in the case of the following xml:

<extended>
  <a />
  <c />
  <b />
</extended>

I tried manually setting the property names to the same in my binding.xjb, but it is complaining because of a naming collision.

Upvotes: 1

Views: 650

Answers (1)

J&#233;r&#244;me Radix
J&#233;r&#244;me Radix

Reputation: 10533

Here, you want to extend the list of possible choices, not the type. To do so, you should use a schema like this :

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

  <xs:element name="base">
    <xs:complexType>
      <xs:choice maxOccurs="unbounded">
        <xs:group ref="baseGroup"/>
      </xs:choice>
    </xs:complexType>
  </xs:element>

  <xs:element name="extended">
    <xs:complexType>
      <xs:choice maxOccurs="unbounded">
        <xs:group ref="extendedGroup"/>
      </xs:choice>
    </xs:complexType>
  </xs:element>

  <xs:group name="baseGroup">
    <xs:choice>
      <xs:element name="a" />
      <xs:element name="b" />
    </xs:choice>
  </xs:group>

  <xs:group name="extendedGroup">
    <xs:choice>
      <xs:group ref="baseGroup" />
      <xs:element name="c" />
    </xs:choice>
  </xs:group>

</xs:schema>

I've tried to validate the following file on http://www.xmlvalidation.com/ with the above xmlschema and it works without error :

<?xml version="1.0" encoding="ISO-8859-1"?>
<extended>
  <a />
  <c />
  <b />
</extended>

Upvotes: 2

Related Questions