Issue with XMLBeans 5.1.0: Unable to Generate XML Elements with Concrete Types

81 Views Asked by At

I'm currently working on a project where I need to generate XML elements with concrete types using XMLBeans 5.1.0. However, I'm facing difficulties in achieving this.

I have an XSD schema that defines an abstract element ChartcValue and several concrete elements (BooleanCharacteristic, DateCharacteristic, IntegerCharacteristic, etc.) that are derived from the abstract element using the substitution group. I have already defined the necessary complex types and substitution groups in my XSD schema.

Here's a snippet of my XSD:

Request.xsd

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

  <xs:element name="Request">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="ID" type="xs:int" />
        <xs:element name="CharacteristicValue" type="CharacteristicValueType" minOccurs="0" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

CharacteristicValues.xsd

<xs:element name="ChartcValue" abstract="true">
  <xs:annotation>
    <xs:documentation xml:lang="en">
      Element that is substituted by a characteristic value.
    </xs:documentation>
  </xs:annotation>
</xs:element>

<xs:element name="TextCharacteristic" type="TextCharacteristicType" substitutionGroup="ChartcValue">
  <xs:annotation>
    <xs:documentation>
      Specification for a Text Characteristic.
    </xs:documentation>
  </xs:annotation>
</xs:element>

The Java classes are generated successfully, but when I try to create instances of the concrete types programmatically and generate XML elements, it only allows me to create instances of the abstract ChartcValue type, not the concrete types derived from it.

Here's an example of the code I'm using to generate the XML elements:

CharacteristicValueType charValue = CharacteristicValueType.Factory.newInstance();
XmlObject obj = charValue.addNewChartcValue();
TextCharacteristicType textCharacteristic = TextCharacteristicType.Factory.newInstance();
textCharacteristic.setShortName(shortName);
textCharacteristic.setStringValue(value);
obj.set(textCharacteristic);

I would greatly appreciate any guidance or suggestions on how to correctly generate XML elements with the concrete types using XMLBeans 5.1.0. Is there something I'm missing in my schema or during the generation process? Any insights or alternative approaches to achieve this would be highly appreciated.

0

There are 0 best solutions below