使用 @XmlPath 和命名空间时从 MOXy JAXB 生成的 XML 架构出错

发布于 2024-11-28 18:21:32 字数 2496 浏览 2 评论 0原文

我正在尝试将 java 模型映射到一些 XML 中。为此,我使用 JAXB 的 MOXy 实现。我在从 java 模型生成有效的 XML 模式时遇到了一些麻烦。我使用以下模型:

@XmlRootElement
public class FooBar {
    @XmlAttribute
    @XmlPath("test/@foo")
    private boolean foo;
}

我使用以下代码生成模式:

public static void main(String[] args) throws Exception {
    final SchemaOutputResolver sor = new SchemaOutputResolver() {
        @Override
        public Result createOutput(String namespaceUri,
                String suggestedFileName) throws IOException {
            return new StreamResult(System.out);
        }
    };

    JAXBContext jc = JAXBContext.newInstance(FooBar.class);
    jc.generateSchema(sor);
}

我使用以下 package-info.java 文件:

@XmlSchema(namespace = "http://moxy.test", elementFormDefault = XmlNsForm.QUALIFIED)
package test.moxy;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

运行测试时,我获得以下模式:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:ns0="http://moxy.test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://moxy.test" elementFormDefault="qualified">
   <xsd:complexType name="big">
      <xsd:sequence>
         <xsd:element name="test" minOccurs="0">
            <xsd:complexType>
               <xsd:sequence/>
               <xsd:attribute ref="ns0:foo"/>
            </xsd:complexType>
         </xsd:element>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:element name="big" type="ns0:big"/>
   <xsd:attribute name="foo" type="xsd:boolean" use="required"/>
</xsd:schema>

可以看出,最后一个属性元素不是人们会期待什么。如果我删除 package-info.java 文件,则会生成以下模式:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="fooBar">
      <xsd:sequence>
         <xsd:element name="test" minOccurs="0">
            <xsd:complexType>
               <xsd:sequence/>
               <xsd:attribute name="foo" type="xsd:boolean" use="required"/>
            </xsd:complexType>
         </xsd:element>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:element name="fooBar" type="fooBar"/>
</xsd:schema>

这是我期望生成的模式。有谁知道为什么会这样?

I am trying to map a java model into some XML. To do this I am using the MOXy implementation of JAXB. I have run into some troubles generating a valid XML Schema from the java model. I use the following model:

@XmlRootElement
public class FooBar {
    @XmlAttribute
    @XmlPath("test/@foo")
    private boolean foo;
}

And I am generating the schema with the following code:

public static void main(String[] args) throws Exception {
    final SchemaOutputResolver sor = new SchemaOutputResolver() {
        @Override
        public Result createOutput(String namespaceUri,
                String suggestedFileName) throws IOException {
            return new StreamResult(System.out);
        }
    };

    JAXBContext jc = JAXBContext.newInstance(FooBar.class);
    jc.generateSchema(sor);
}

And I use the following package-info.java file:

@XmlSchema(namespace = "http://moxy.test", elementFormDefault = XmlNsForm.QUALIFIED)
package test.moxy;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

When running the test I obtain the following schema:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:ns0="http://moxy.test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://moxy.test" elementFormDefault="qualified">
   <xsd:complexType name="big">
      <xsd:sequence>
         <xsd:element name="test" minOccurs="0">
            <xsd:complexType>
               <xsd:sequence/>
               <xsd:attribute ref="ns0:foo"/>
            </xsd:complexType>
         </xsd:element>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:element name="big" type="ns0:big"/>
   <xsd:attribute name="foo" type="xsd:boolean" use="required"/>
</xsd:schema>

It can be seen that the last attribute element is not what one would expect. If I remove the package-info.java file, the following schema is generated:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="fooBar">
      <xsd:sequence>
         <xsd:element name="test" minOccurs="0">
            <xsd:complexType>
               <xsd:sequence/>
               <xsd:attribute name="foo" type="xsd:boolean" use="required"/>
            </xsd:complexType>
         </xsd:element>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:element name="fooBar" type="fooBar"/>
</xsd:schema>

Which is what I would expect being generated. Does anyone know why this is the case?

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

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

发布评论

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

评论(1

笑红尘 2024-12-05 18:21:32

更新

此问题 (https://bugs.eclipse.org/354130)该问题现已在我们的 EclipseLink 2.3.1 和 2.4.0 流中得到修复,并将于 2011 年 8 月 9 日起从我们的夜间下载网站提供:

UPDATE

This issue (https://bugs.eclipse.org/354130) has now been fixed in our EclipseLink 2.3.1 and 2.4.0 streams, and will be available from our nightly download site starting August 9th, 2011:

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