JAXB 将命名空间添加到父元素,但不添加到所包含的子元素

发布于 2024-09-03 18:48:14 字数 2566 浏览 4 评论 0原文

我组装了一个 XSD 并使用 JAXB 从中生成类。

这是我的 XSD-

myDoc.xsd :
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://www.mydoc.org"
       targetNamespace="http://www.mydoc.org"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:mtp="http://www.mytypes.com" elementFormDefault="qualified">

<xs:import namespace="http://www.mytypes.com" schemaLocation="mytypes.xsd" />
<xs:element name="myDoc">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="crap" type="xs:string"/>
      <xs:element ref="mtp:foo"/>
      <xs:element ref="mtp:bar"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

mytypes.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.mytypes.com"
       xmlns="http://www.mytypes.com"
       xmlns:tns="http://www.mytypes.com"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       attributeFormDefault="qualified" elementFormDefault="qualified">


  <xs:element name="foo" type="tns:Foo"/>
  <xs:element name="bar" type="tns:Bar"/>
  <xs:element name="spam" type="tns:Spam"/>

  <xs:simpleType name="Foo">
    <xs:restriction base="xs:string"></xs:restriction>
  </xs:simpleType>

  <xs:complexType name="Bar">
    <xs:sequence>
      <xs:element ref="spam"/>
    </xs:sequence>
  </xs:complexType>

  <xs:simpleType name="Spam">
    <xs:restriction base="xs:string" />
  </xs:simpleType>

</xs:schema>

编组的文档是 -

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myDoc xmlns:ns2="http://www.mytypes.com">
  <crap>real crap</crap>
  <ns2:foo>bleh</ns2:foo>
  <ns2:bar>
    <spam>blah</spam>
  </ns2:bar>
</myDoc>

请注意, 元素使用默认名称空间。我希望它使用 ns2 命名空间。架构 (mytypes.xsd) 表示 包含在 中,在 XML 实例中绑定到 ns2< /代码> 命名空间。

我已经为此绞尽脑汁一个多星期了,我希望 ns2 前缀出现在 中。我应该怎么办?

必需的 :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myDoc xmlns:ns2="http://www.mytypes.com">
  <crap>real crap</crap>
  <ns2:foo>bleh</ns2:foo>
  <ns2:bar>
    <ns2:spam>blah</ns2:spam><!--NS NS NS-->
  </ns2:bar>
</myDoc>

I put together an XSD and used JAXB to generate classes out of it.

Here are my XSDs-

myDoc.xsd :
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://www.mydoc.org"
       targetNamespace="http://www.mydoc.org"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:mtp="http://www.mytypes.com" elementFormDefault="qualified">

<xs:import namespace="http://www.mytypes.com" schemaLocation="mytypes.xsd" />
<xs:element name="myDoc">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="crap" type="xs:string"/>
      <xs:element ref="mtp:foo"/>
      <xs:element ref="mtp:bar"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

mytypes.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.mytypes.com"
       xmlns="http://www.mytypes.com"
       xmlns:tns="http://www.mytypes.com"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       attributeFormDefault="qualified" elementFormDefault="qualified">


  <xs:element name="foo" type="tns:Foo"/>
  <xs:element name="bar" type="tns:Bar"/>
  <xs:element name="spam" type="tns:Spam"/>

  <xs:simpleType name="Foo">
    <xs:restriction base="xs:string"></xs:restriction>
  </xs:simpleType>

  <xs:complexType name="Bar">
    <xs:sequence>
      <xs:element ref="spam"/>
    </xs:sequence>
  </xs:complexType>

  <xs:simpleType name="Spam">
    <xs:restriction base="xs:string" />
  </xs:simpleType>

</xs:schema>

The document marshalled is-

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myDoc xmlns:ns2="http://www.mytypes.com">
  <crap>real crap</crap>
  <ns2:foo>bleh</ns2:foo>
  <ns2:bar>
    <spam>blah</spam>
  </ns2:bar>
</myDoc>

Note that the <spam> element uses the default namespace. I would like it to use the ns2 namespace. The schema (mytypes.xsd) expresses the fact that <spam> is contained within <bar> which in the XML instance is bound to the ns2 namespace.

I've broken my head over this for over a week and I would like ns2 prefix to appear in <spam>. What should I do?

Required :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myDoc xmlns:ns2="http://www.mytypes.com">
  <crap>real crap</crap>
  <ns2:foo>bleh</ns2:foo>
  <ns2:bar>
    <ns2:spam>blah</ns2:spam><!--NS NS NS-->
  </ns2:bar>
</myDoc>

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

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

发布评论

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

评论(6

风向决定发型 2024-09-10 18:48:14

检查生成的类中的字段是否缺少 @XmlElement 注释,如果存在,是否缺少名称空间属性。为了获取编组 xml 中每个元素的命名空间前缀,这两个是必须的

check if the fields in a generated classed are missing the @XmlElement annotation and if present are they missing the namespace attribute. These two are must in order to get the namespace prefix against every element in your marshaled xml

心凉 2024-09-10 18:48:14

我试图重现您的问题,但在这里它工作正常:当编组垃圾邮件元素时确实获得了 ns2 命名空间。

我的编组代码:

    Bar bar = new Bar();
    bar.setSpam("s");

    MyDoc myDoc = new MyDoc();
    myDoc.setBar(bar);

    JAXBContext context = JAXBContext.newInstance("org.mydoc");

    Marshaller marshaller = context.createMarshaller();
    marshaller.marshal(myDoc, System.out);

输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><myDoc xmlns="http://www.mydoc.org" xmlns:ns2="http://www.mytypes.com"><ns2:bar><ns2:spam>s</ns2:spam></ns2:bar></myDoc>

我的 JAXB 版本:

xjc version "JAXB 2.1.3 in JDK 1.6"
JavaTM Architecture for XML Binding(JAXB) Reference Implementation, (build IBM JAXB 2.1.3 in JDK 1.6)

编辑:

Bar.java 类确实具有以下注释:

@XmlElement(required = true)
protected String spam;

XmlElement 还具有名称空间属性。 Javadoc: http: //java.sun.com/javase/6/docs/api/javax/xml/bind/annotation/XmlElement.html#namespace()

默认情况下,它将查看 com.mytypes 中的 @XmlSchema 注释包裹。您是否删除了 @XmlSchema 注释和/或 package-info.java 文件?

I have tried to reproduce your problem, but here it's working correctly: When marshalling the spam element does get a ns2 namespace.

My marshalling code:

    Bar bar = new Bar();
    bar.setSpam("s");

    MyDoc myDoc = new MyDoc();
    myDoc.setBar(bar);

    JAXBContext context = JAXBContext.newInstance("org.mydoc");

    Marshaller marshaller = context.createMarshaller();
    marshaller.marshal(myDoc, System.out);

Output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><myDoc xmlns="http://www.mydoc.org" xmlns:ns2="http://www.mytypes.com"><ns2:bar><ns2:spam>s</ns2:spam></ns2:bar></myDoc>

My JAXB version:

xjc version "JAXB 2.1.3 in JDK 1.6"
JavaTM Architecture for XML Binding(JAXB) Reference Implementation, (build IBM JAXB 2.1.3 in JDK 1.6)

EDIT:

The Bar.java class does have the following annotation:

@XmlElement(required = true)
protected String spam;

The XmlElement also has a namespace attribute. Javadoc: http://java.sun.com/javase/6/docs/api/javax/xml/bind/annotation/XmlElement.html#namespace()

By default it will look at the @XmlSchema annotation in the com.mytypes package. Did you remove the @XmlSchema annotation and/or the package-info.java file?

じее 2024-09-10 18:48:14

package-info.java 没有被我的构建系统编译,因此 elementFormDefault="qualified" 并且目标命名空间没有进入 Jaxb 带注释的类。

如果您看到此行为,请确保您的 package-info.java 正在编译为 .class 文件并且位于您的类路径中。

干杯。

package-info.java was not being compiled by my build system and hence elementFormDefault="qualified" and the target namespace did not make it to the Jaxb annotated classes.

If you see this behavior, make sure your package-info.java is being compiled into a .class file and is in your classpath.

Cheers.

浅唱ヾ落雨殇 2024-09-10 18:48:14

如果您使用 Maven 2 和以下 jaxws-maven-plugin 进行构建,也会出现此错误。

 <groupId>org.codehaus.mojo</groupId>
 <artifactId>jaxws-maven-plugin</artifactId>

并且出于某种原因为 java 1.5 配置了 maven-compiler-plugin。将目标/源设置为 1.6 可以解决该问题。

 <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
        </configuration>
 </plugin>

如果您知道原因,请随时评论。

This Error also occurs if you build with Maven 2 and the following jaxws-maven-plugin.

 <groupId>org.codehaus.mojo</groupId>
 <artifactId>jaxws-maven-plugin</artifactId>

And for some reason have configured the maven-compiler-plugin for java 1.5. Setting target/source to 1.6 fixes the problem.

 <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
        </configuration>
 </plugin>

If you know why, please feel free to comment.

揪着可爱 2024-09-10 18:48:14

您可以在 package-info.java 中指定它
在 @XmlSchema 注释中。

elementFormDefault=XmlNsForm.QUALIFIED

示例包信息.java

@XmlSchema(
    namespace="desiredNamespace",   
    // If qualified namespace will be added to all elements
    elementFormDefault = XmlNsForm.QUALIFIED,
    // If qualifies namespace will be added to all attributes
    attributeFormDefault = XmlNsForm.UNQUALIFIED,
    xmlns = {   
        @XmlNs(prefix = "xsd", namespaceURI = "desiredNamespace"),
        @XmlNs(prefix = "xsi", namespaceURI = "anotherLink"),
    }
)

You can specify it in your package-info.java
in @XmlSchema annotation.

elementFormDefault=XmlNsForm.QUALIFIED

sample package-info.java

@XmlSchema(
    namespace="desiredNamespace",   
    // If qualified namespace will be added to all elements
    elementFormDefault = XmlNsForm.QUALIFIED,
    // If qualifies namespace will be added to all attributes
    attributeFormDefault = XmlNsForm.UNQUALIFIED,
    xmlns = {   
        @XmlNs(prefix = "xsd", namespaceURI = "desiredNamespace"),
        @XmlNs(prefix = "xsi", namespaceURI = "anotherLink"),
    }
)
静若繁花 2024-09-10 18:48:14

您检查您的代码是否已将 ElementType 指定为“垃圾邮件”,但在栏下称为“垃圾邮件”。你的代码中的这个大写“S”和小写“s”,如果不打扰,我很抱歉,但如果它真的打扰,你可能必须正确使用它。尝试在所有地方使用“垃圾邮件”并检查一下...

-- 抱歉,我是 xslt、xml、xsd 等方面的初学者,对 JAXB 一无所知。但一般来说,当我们阅读有关使用 Xsd 编写 xml 文档时,我们也会始终注意区分大小写。

抱歉,如果这个答案不能解决您的问题。

Did u check your code that you have specified your ElementType as "Spam" but referred under bar as "spam". this capital "S" and small "s" in ur code, if doesnt bother, i am sorry but if it really bothers, u may have to use it properly. Try use "Spam" across everywhere and check it out na..


-- sorry, i am a beginner in xslt, xml, xsd and stuffs and no knowledge about JAXB. but in general, when we read about writing xml documents using Xsd's, we would always take care of case sensitiveness as well.

sorry if this answer doesnt solve ur problem.

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