Problem Creating Schema Using @XmlElementRef

1.6k Views Asked by At

I'm trying to duplicate a bit of schema by starting with Java and using schemagen (long story). The schema looks like this:

<xsd:element name="Responses">
   <xsd:complexType>
      <xsd:choice maxOccurs="unbounded">
         <xsd:element ref="tns:Accepted"/>
         <xsd:element ref="tns:Rejected"/>
         <xsd:element ref="oth:Exception"/>
      </xsd:choice>
   </xsd:complexType>
</xsd:element>

<xsd:element name="Accepted" type="tns:ResponseType"/>
<xsd:element name="Rejected" type="tns:ResponseType"/>

<xsd:complexType name="ResponseType">
   <xsd:sequence>
     <xsd:element name="Id" type="xsd:anyURI"/>
     <!-- more valid elements -->
  </xsd:sequence>
</xsd:complexType>

I created the following class to represent it:

@XmlRootElement(name="Responses")
public class Responses {

    @XmlElementRefs(value = {
            @XmlElementRef(name="Accepted", namespace="http://sample.net", type=ResponseType.class),
            @XmlElementRef(name="Rejected", namespace="http://sample.net", type=ResponseType.class),
            @XmlElementRef(name="Exception", namespace="http://other.net", type=ExceptionType.class)
    })
    public List<JAXBElement<Object>> response;
}

"Accepted" and "Rejected" are only defined in the ObjectFactory and have no class backing the name:

@XmlElementDecl(namespace = "http://sample.net", name = "Accepted")
public JAXBElement<ResponseType> createAccepted(ResponseType value) {
    return new JAXBElement<ResponseType>(_Accepted_QNAME, ResponseType.class, null, value);
}

When I run these through schemagen, I get the error

error: XmlElementRef points to a non-existent class.

I know the problem is with "Accepted" and "Rejected", but how do I resolve it? Do I NEED classes backing those element names? For example, if I make a class like this:

@XmlRootElement(name="Accepted")
public class Accepted extends ResponseType { }

schemagen accepts it. But, why would I need that implementation?

1

There are 1 best solutions below

1
On

Yes, you need to define the type ResponseType.

something like this:

<xs:complexType name="ResponseType">
    <xs:choice maxOccurs="unbounded">
        <xs:element name="name" type="xs:string"/>
        <xs:any namespace="##other" processContents="lax"/>
    </xs:choice>
</xs:complexType>

your final elements will look like this: some string some string

or

<xs:simpleType name="ResponseType">
    <xs:restriction base="xs:string"/>
</xs:simpleType>

would have somestring