JAXB:限定属性禁用默认命名空间 xmlns=“”?

发布于 2024-11-26 08:34:23 字数 541 浏览 2 评论 0原文

当我使用 @XmlSchema(attributeFormDefault = XmlNsForm.QUALIFIED, ...)

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

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

而不是:

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

生成类似以下内容:

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

这是预期的行为吗?有什么办法可以纠正这个问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦毁影碎の 2024-12-03 08:34:23

EclipseLink JAXB (MOXy) 正在处理元素的前缀限定根据属性形式限定而有所不同(如下所示)。

命名空间限定没有错误,但我同意尽可能使用默认命名空间更好。您可以使用以下错误跟踪此问题的进展:

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;
    }

}

演示

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);
    }

}

未设置 attributeFormDefault 的包信息

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

import javax.xml.bind.annotation.*;

输出:

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

包-info 与 attributeFormDefault设置

@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.*;

输出:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文