【问题标题】:XSD to jaxb annotated classes gives cannot resolve xml element errorXSD 到 jaxb 注释类给出了无法解析 xml 元素错误
【发布时间】:2013-08-01 17:34:23
【问题描述】:
我有无错误的 xsd。下面是我的xsd。我只是指主要元素。 Jaxb 转换的类在 @XmlRootElement(name = "principal") 上给出错误:“无法在此上下文中使用命名空间 '' 和名称 'principal' 解析 XML 元素声明”。有人可以帮忙吗?
这是我的 xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="authorization"><xs:complexType> <xs:sequence><xs:element name="group" maxOccurs="unbounded" minOccurs="1"><xs:complexType><xs:sequence><xs:element ref="principal" maxOccurs="unbounded" minOccurs="1" /></xs:sequence>
<xs:attribute name="name" type="xs:string"></xs:attribute>
</xs:complexType></xs:element> <xs:element name="principal" ><xs:complexType><xs:simpleContent><xs:extension base="xs:string"><xs:attribute type="xs:string" name="family" use="optional"/><xs:attribute name="type" type="xs:string" use="optional"/>
</xs:extension></xs:simpleContent></xs:complexType></xs:element>
</xs:schema>
【问题讨论】:
标签:
jaxb
xsd
jaxb2
xsd.exe
xsd-1.1
【解决方案1】:
首先,您的架构是错误的。我认为您尝试这样写:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="authorization">
<xs:complexType>
<xs:sequence>
<xs:element name="group" maxOccurs="unbounded" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element ref="principal" maxOccurs="unbounded"
minOccurs="1" />
</xs:sequence>
<xs:attribute name="name" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="principal">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="family" use="optional" />
<xs:attribute name="type" type="xs:string" use="optional" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
对于这个 XSD,我也遇到了同样的问题。如果您更改为其他 XSD,问题将得到解决
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="authorization">
<xs:complexType>
<xs:sequence>
<xs:element name="group" maxOccurs="unbounded" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element ref="principal" maxOccurs="unbounded"
minOccurs="1" />
</xs:sequence>
<xs:attribute name="name" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="principal">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="family" use="optional" />
<xs:attribute name="type" type="xs:string" use="optional" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
问候
亚历克斯。