JAXB: Qualified attributes disables default namespace xmlns=""?
Asked Answered
F

1

5

When I use @XmlSchema(attributeFormDefault = XmlNsForm.QUALIFIED, ...)

or@XmlAttribute(namespace = "sample.com/y", ...)

JAXB ignores @XmlSchema(namespace = "sample.com/x", ...)

and instead of:

<a xmlns="sample.com/y" xmlns:ns0="sample.com/y">
  <b ns0:att=""/>
</a>

generates something like:

<ns1:a xmlns:ns1="sample.com/x" xmlns:ns0="sample.com/y">
  <ns1:b ns0:att=""/>
</ns1:a>

Is this an expected behavior? Is there any way to correct this?

Fucus answered 24/7, 2011 at 18:50 Comment(0)
L
4

EclipseLink JAXB (MOXy) is handling the prefix qualification for elements differently depending upon the attribute form qualification (as demonstrated below).

The namespace qualification is not wrong, but I agree that the use of default namespace is better when possible. You can track the progress on this issue using the following bug:

A

package forum6808921;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class A {

    private String b;

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }

}

Demo

package forum6808921;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(A.class);

        A a = new A();
        a.setB("Hello World");

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(a, System.out);
    }

}

package-info without attributeFormDefault set

@XmlSchema(
        namespace = "sample.com/x"
        , elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED
        )
package forum6808921;

import javax.xml.bind.annotation.*;

Output:

<?xml version="1.0" encoding="UTF-8"?>
<a xmlns="sample.com/x">
   <b>Hello World</b>
</a>

package-info with attributeFormDefault set

@XmlSchema(
        namespace = "sample.com/x"
        , elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED
        , attributeFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED
        )
package forum6808921;

import javax.xml.bind.annotation.*;

Output:

<?xml version="1.0" encoding="UTF-8"?>
<ns0:a xmlns:ns0="sample.com/x">
   <ns0:b>Hello World</ns0:b>
</ns0:a>
Lorenalorene answered 25/7, 2011 at 20:6 Comment(1)
Thanks for confirming the issue.Fucus

© 2022 - 2024 — McMap. All rights reserved.