【发布时间】:2011-04-17 21:21:18
【问题描述】:
我有一个定义以下架构的 XSD:
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://example.com/2010/aa/"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:aa="http://example.com/2010/aa/"
targetNamespace="http://example.com/2010/aa/"
elementFormDefault="qualified">
...
<xs:element name="user" type="aa:User"/>
<xs:complexType name="User">
<xs:sequence>
<xs:element ref="aa:firstName" minOccurs="0" maxOccurs="1"/>
<xs:element ref="aa:lastName" minOccurs="0" maxOccurs="1"/>
...
<xs:any namespace="##targetNamespace" processContents="skip" maxOccurs="unbounded" />
</xs:sequence>
<xs:anyAttribute processContents="skip" />
</xs:complexType>
<xs:element name="profile" type="aa:Profile"/>
<xs:complexType name="Profile">
<xs:sequence>
<xs:element ref="aa:username" minOccurs="0" maxOccurs="1"/>
<xs:element ref="aa:accountStatus" minOccurs="0" maxOccurs="1" />
<xs:element ref="aa:roleid" minOccurs="0" maxOccurs="1"/>
...
<xs:element ref="aa:userid"/>
</xs:sequence>
<xs:anyAttribute processContents="skip" />
</xs:complexType>
当 JAXB 编组生成的对象时,它定义以下内容:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<user xmlns:ns2="http://example.com/2010/aa/">...</user>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<profile xmlns="http://example.com/2010/aa/">...</profile>
看看一个命名空间是xmlns:ns2,另一个是xmlns。这是因为 user 的所有元素都符合 aa 命名空间,但 xs:any 标签定义的元素除外,因此需要定义两个命名空间。
Profile 没有 xs:any 标签,也不需要定义多个命名空间。这是我的解释,因为如果我从用户定义中删除 xs:any,它将从生成的 XML 中删除 ns2。
我如何告诉 JAXB targetNamespace 和 aa 是同一个命名空间,所以它不包含两者?
【问题讨论】: