Open
Description
XSD schema's support a way to name an element based on its runtime type through substitution groups. I'm trying to deserialise such a substitution group. Is this supported in some way?
Example. Suppose there is two types of Animal
s: Cow
s and Goat
s. Based on their type, I want their element to be named either cow
or goat
. This would be modelled as follows:
XSD:
<!-- XML root element -->
<xs:element name="document">
<xs:complexType>
<xs:sequence>
<xs:element ref="animal"/> <!-- Note that the name of this element is determined by the actual type of animal -->
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- Element names: `Cow`s should be serialised as an element named `cow`, `Goat`s should be serialised as an element named `goat`. -->
<xs:element name="animal" type="Animal" abstract="true" />
<xs:element name="cow" type="Cow" substitutionGroup="animal" />
<xs:element name="goat" type="Goat" substitutionGroup="animal" />
<!-- Types: `Cow` and `Goat` extend from `Animal` -->
<xs:complexType name="Animal">
<xs:attribute name="name" type="xs:string" />
</xs:complexType>
<xs:complexType name="Cow">
<xs:complexContent>
<xs:extension base="Animal" />
</xs:complexContent>
</xs:complexType>
<xs:complexType name="Goat">
<xs:complexContent>
<xs:extension base="Animal" />
</xs:complexContent>
</xs:complexType>
XML example instances:
<document>
<cow name="Cow1" />
</document>
<document>
<goat name="Goat1" />
</document>
Java model:
@XmlRootElement(name="document")
public class Document {
private Animal animal;
public Animal getAnimal() {
return animal;
}
public void setAnimal(Animal animal) {
this.animal = animal;
}
}
public class Animal {
@JacksonXmlProperty(isAttribute = true)
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Cow extends Animal {}
public class Goat extends Animal {}
Is there a way of annotating the classes to get the desired behaviour?
Metadata
Metadata
Assignees
Labels
No labels