【发布时间】:2010-05-31 12:54:16
【问题描述】:
我组装了一个 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>
请注意,<spam> 元素使用默认命名空间。我希望它使用 ns2 命名空间。架构 (mytypes.xsd) 表示 <spam> 包含在 <bar> 中,在 XML 实例中绑定到 ns2 命名空间。
我已经为此烦恼了一个多星期,我希望ns2 前缀出现在<spam> 中。我该怎么办?
必填:
<?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>
【问题讨论】:
标签: java xml xsd jaxb marshalling