Reputation: 2394
We are having a problem with xsi:type since upgrading our server to jaxb 2. The xml client request in question was parsed ok when the server was running jaxb 1, but since upgrading to jaxb 2 we are seeing an error along the lines of:
Error::cvc-elt.4.2: Cannot resolve 'er-request' to a type definition for element 'er-request'
A client is specifying xsi:type
as an attribute in a xml tag, but I don't think it's valid since the complex type in question has no such name.
<?xml version="1.0" encoding="UTF-8"?>
<er-request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="100007" xsi:type="er-request">
<payload>
<!--some content-->
</payload>
</er-request>
Here is the xsd:
<xs:element name="er-request">
<xs:complexType>
<xs:sequence>
<xs:element name="payload" type="payloadType" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="id" use="required">
<xs:simpleType>
<xs:restriction base="xs:int"/>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:complexType name="payloadType">
<xs:sequence>
<xs:any processContents="skip" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
So as you can see there is no name
attribute in the complexType. We can work around the error by adding it, but then we see a validation error since apparently a name attribute on a local complexType:
Attribute 'name' cannot appear in element 'complexType'
I presume the reason this worked in jaxb1 was that it was less strict than jaxb 2.
So the questions are:
Upvotes: 2
Views: 1980
Reputation: 149017
No, as you state the value of the xsi:type
attribute does not correspond to a named complex type in your XML schema.
JAXB implementations aim to be very fault tolerant. I couldn't get your example to fail using the default JAXB impl in the latest JDK 1.7 install or EclipseLink JAXB (MOXy):
ErRequest
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="er-request")
public class ErRequest {
}
Demo
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(ErRequest.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StreamSource xml = new StreamSource("src/forum22497112/input.xml");
ErRequest erRequest = (ErRequest) unmarshaller.unmarshal(xml);
}
}
The error you are getting appears to be a schema validation error:
Error::cvc-elt.4.2: Cannot resolve 'er-request' to a type definition for element 'er-request'
Are you asking JAXB to perform schema validation on the input? JAXB does not do this by default. Since your document is not valid you will have to fix it or register an error listener to ignore it.
Upvotes: 2
Reputation: 163342
You need to promote the anonymous complex type to a global (named) type:
<xs:element name="er-request" type="er-request"/>
<xs:complexType name="er-request">
<xs:sequence>
<xs:element name="payload" type="payloadType" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="id" use="required">
<xs:simpleType>
<xs:restriction base="xs:int"/>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
Alternatively, just remove the xsi:type attribute. It's not doing anything useful here since the element name is enough to identify the required type.
Upvotes: 1