XSD: imported elements of a different namespace take up the target namespace of the parent when creating a POJO via the Jaxb plugin

I am trying to generate POJOs via maven JAXB parsing XML plugin that I have. I have a root level element in my XML that has a namespace that is different from the other elements inside it. Below is the XML:

        <?xml version="1.0" encoding="UTF-8"?>
<skuFlatFileType xmlns="http://www.abc/schemas/xyz/sdf/Schemas/Schema.xsd" xmlns:ns0="http://www.xyz/schemas/dbm/product/V1">
       <FlattenedSKU>
          <ns0:SKU></ns0:SKU>
</FlattenedSKU>
</skuFlatFileType>

      

Since FlattenedSKU and SKU are in a different namespace. So I declared a separate XSD for all elements under FlattenedSKU and then imported it inside the parent. The XSD looks like this:

sku_wrapper.xsd

<xs:schema
    xmlns="http://www.abc/schemas/xyz/sdf/Schemas/Schema.xsd"
     targetNamespace="http://www.abc/schemas/xyz/sdf/Schemas/Schema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified"
    xmlns:ns0="http://www.xyz/schemas/dbm/product/V1">
    <xs:import namespace="http://www.xyz/schemas/dbm/product/V1"
        schemaLocation="sku.xsd" />
    <xs:element name="FlattenedSKU">
         <xs:complexType>
            <xs:sequence>
                <xs:element name="SKU" type="ns0:SKU"/>
            </xs:sequence>
        </xs:complexType>
        </xs:element>
       </xs:schema>

      

The xsd child looks like this:

sku.xsd

<xs:schema attributeFormDefault="qualified"
    elementFormDefault="qualified"
    xmlns="http://www.xyz/schemas/dbm/product/V1"
    targetNamespace="http://www.xyz/schemas/dbm/product/V1"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="SKU">
        <xs:sequence>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

      

Now the generated java class looks like this:

@XmlRootElement(name = "FlattenedSKU", namespace = "http://www.abc/schemas/xyz/sdf/Schemas/Schema.xsd")
public class FlattenedSKU {

@XmlElement(name = "SKU", namespace = "http://www.abc/schemas/xyz/sdf/Schemas/Schema.xsd", required = true)
protected SKU sku;

      

But I need the following:

@XmlRootElement(name = "FlattenedSKU", namespace = "http://www.abc/schemas/xyz/sdf/Schemas/Schema.xsd")
public class FlattenedSKU {

@XmlElement(name = "SKU", namespace = "http://www.xyz/schemas/dbm/product/V1", required = true)
protected SKU sku;

      

Can someone tell me what I am doing wrong?

+3


source to share


1 answer


In addition to my comment, what you should do is "ref" for element in sku.xsd

and not create element insku_wrapper.xsd

So the following will work for you:

sku.xsd

 <xs:schema attributeFormDefault="qualified"
    elementFormDefault="qualified"
    xmlns="http://www.xyz/schemas/dbm/product/V1"
    targetNamespace="http://www.xyz/schemas/dbm/product/V1"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="SKU">
        <xs:sequence>
        </xs:sequence>
    </xs:complexType>
     <xs:element name="SKU" type="SKU"/> 
</xs:schema>

      



Note that the change is an element declaration here.

sku_wrapper.xsd

<xs:schema
    xmlns="http://www.abc/schemas/xyz/sdf/Schemas/Schema.xsd"
     targetNamespace="http://www.abc/schemas/xyz/sdf/Schemas/Schema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified"
    xmlns:ns0="http://www.xyz/schemas/dbm/product/V1">
   <xs:import namespace="http://ww

w.xyz/schemas/dbm/product/V1"
        schemaLocation="sku.xsd" />
    <xs:element name="FlattenedSKU">
         <xs:complexType>
            <xs:sequence>
                   <xs:element ref="ns0:SKU"/>
            </xs:sequence>
        </xs:complexType>
        </xs:element>
  </xs:schema>

      

Notice the item link here

+1


source







All Articles