使用 @XmlPath 和命名空间时从 MOXy JAXB 生成的 XML 架构出错
我正在尝试将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新
此问题 (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: