3

I am trying to generate POJOs via the maven JAXB plugin to parse the XML I have got. I have a root level element in my XML which has a namespace which is different from the other elements inside it. Following 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 the different namespace. So I declared a separate XSD for all elements under the FlattenedSKU and then imported it inside the parent one. The XSD look 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 child xsd is as follows:

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 what I need is as follows:

@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 somebody tell me what I am doing wrong ?

Aryan Singh
  • 87
  • 2
  • 12
  • Isn't that correct generation of jaxb? Your element SKU is defined in namespace `"http://www.abc/schemas/xyz/sdf/Schemas/Schema.xsd` itself. Only it's TYPE is defined in a different namespace as per your schema – Optional May 11 '17 at 07:03
  • I am not really sure. I followed this answer: http://stackoverflow.com/questions/6413145/xsd-with-elements-from-other-namespace but it didn't work. Do you have any idea on how to achieve what I am trying to do ? – Aryan Singh May 11 '17 at 07:21
  • Just posted my answer. Please understand the difference between "creating" an element of type vs actually referring to it. When you create an element of a type defined in other schema, this newly created element belongs to the namespace of schema that you are adding – Optional May 11 '17 at 07:23

1 Answers1

1

In Addition to my comment what you shall do is "ref" to an element in sku.xsd and not create an element in sku_wrapper.xsd

So following shall 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 change is declaration of element 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 reference of element here

Optional
  • 4,387
  • 4
  • 27
  • 45
  • Yes the ref attribute does the job but now namespace does not appear in the child elements of the generated POJO. @XmlRootElement(name = "FlattenedSKU", namespace = "http://www.abc/schemas/xyz/sdf/Schemas/Schema.xsd") public class FlattenedSKU { @XmlElement(name = "SKU", required = true) protected SKU sku; – Aryan Singh May 11 '17 at 09:58
  • @AryanSingh Did you get past this? – Optional May 12 '17 at 06:23
  • Yup got past the issue. As mentioned in above comment couldn't get the namespace on the child element though. – Aryan Singh May 12 '17 at 09:35
  • Surprising. I can see the namespace for child as well. Otherwise the model generation does not mean correct. Or may be your child class has namespace defined in its @XmlElement tag – Optional May 12 '17 at 11:27