开发者

JAXB generating wrong Namespace

I have a xsd (lets name it as afb) which imports another xsd (lets name it as kts). And I refer to an element of kts.xsd in afb.xsd along with correct namespaces.

But when I generate the classes using JAXB, the namespace for refered element is wrong.

I mean, the referred element should开发者_如何转开发 have kts namespace where as it is having afb namespace.

Because of which validating my XML against this xsd is failing also not able to bind the xml data into the java models.

EX: afb.xsd :

<xs:import namespace="http://www.boschkts.com" schemaLocation="kts.xsd"/>
<xs:element name="vehicle">

<xs:complexType>
        <xs:sequence>
          <xs:element ref="vType"/>
          <xs:element name="RESULTS" type="kts:RESULTS" >
        </xs:sequence>
    </xs:complexType>
</xs:element>

kts:xsd :

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.boschkts.com" 
targetNamespace="http://www.boschkts.com" elementFormDefault="qualified">

<xs:complexType name="RESULTS">
    <xs:sequence>
        <xs:element name="SUMMARY" type="SUMMARY" minOccurs="0" />
    </xs:sequence>
</xs:complexType>

Vehicle.java :

public class Vehicle {

    @XmlElement(namespace = "http://www.boschafb.com", required = true)
    protected String vType;
    @XmlElement(name = "RESULTS", namespace = "http://www.boschafb.com", required = true)
    protected Results results;

}

If I observe the Vehicle.java, the namespace of results property should have been "http://www.boschkts.com" instead of "http://www.boschafb.com"

If I change the namespace manually then binding the data from xml to java models works. But still validating against the xsd fails with the error :

Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'kts:RESULTS'. One of '{"http://www.boschafb.com":RESULTS}' is expected.

Can anybody point what I might be missing in the xsd? or is it the way jaxb generates and I have to modify the classes manually?

Regards,

Satya


I'm assuing your abf.xsd starts with

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.boschafb.com"
           targetNamespace="http://www.boschafb.com" elementFormDefault="qualified">

With elementFormDefault set to qualified, all element declarations, even nested ones, belong to the specified target namespace. Note that this applies only to elements, a referenced type does not affect the namespace of the element referencing it.

A solution would be to define an element instead of a type in 'kts.xsd' and referencing this element in your first schema:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.boschkts.com" 
           targetNamespace="http://www.boschkts.com" elementFormDefault="qualified">
    <xs:element name="RESULTS">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="SUMMARY" type="SUMMARY" minOccurs="0" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.boschafb.com"
           targetNamespace="http://www.boschafb.com" elementFormDefault="qualified"
           xmlns:kts="http://www.boschkts.com">

    <xs:import namespace="http://www.boschkts.com" schemaLocation="kts.xsd"/>

    <xs:element name="vehicle">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="vType"/>
                <xs:element ref="kts:RESULT"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>


Try using an include instead of an import like so:

<xs:include schemaLocation="kts.xsd"/> instead of <xs:import namespace="http://www.boschkts.com" schemaLocation="kts.xsd"/>

This style causes far less problems with intra-namespace includes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜