Skip to content

Support XML element name based on runtime type #679

Open
@SimonCockx

Description

@SimonCockx

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 Animals: Cows and Goats. 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?

Activity

cowtowncoder

cowtowncoder commented on Oct 29, 2024

@cowtowncoder
Member

This is a known "most wanted feature", not specific to XML Schema (as in, supported by JAXB and other xml tools). Unfortunately such "flattening" (of nested elements) is not really supported by Jackson XML module.
It would be great to support it, but problem is that requires kind of structural transformation that is currently difficult to achieve due to the way Type[De]Serializers and regular Json[De]Serializers interact.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @cowtowncoder@SimonCockx

        Issue actions

          Support XML element name based on runtime type · Issue #679 · FasterXML/jackson-dataformat-xml