如何解决JAXB - 当属性已经定义并使用绑定时,它不适用于 *complexContent*
使用 JAXB 生成 Java 类。 我正在使用以下 XSD 方案。
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://schemas.microsoft.com/developer/msbuild/2003" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msb="http://schemas.microsoft.com/developer/msbuild/2003" elementFormDefault="qualified">
<!-- Working -->
<xs:complexType name="MetaTypeSimpleContent">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="Name" />
<xs:attribute type="xs:string" name="Scheme" />
<xs:attribute type="xs:string" name="Value" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<!-- Not Working -->
<xs:complexType name="MetaTypeComplexContent">
<xs:complexContent>
<xs:extension base="msb:BaseType">
<xs:attribute name="MyName" type="xs:string" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="BaseType">
<xs:attribute name="MyName" type="xs:string" />
</xs:complexType>
</xs:schema>
..和以下绑定定义
<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.1">
<bindings schemaLocation="Test.xsd" version="1.0">
<!-- Customise the package name -->
<schemaBindings>
<package name="com.example.schema" />
</schemaBindings>
<!-- Working -->
<bindings node="//xs:complexType[@name='MetaTypeSimpleContent']">
<bindings node=".//xs:attribute[@name='Value']">
<property name="ValueAttribute" />
</bindings>
</bindings>
<!-- Not Working -->
<bindings node="//xs:complexType[@name='MetaTypeComplexContent']/xs:complexContent/xs:extension/xs:attribute[@name='MyName']">
<property name="MyName3" />
</bindings>
</bindings>
</bindings>
complextType MetaTypeSimpleContent 与 simpleContent 的绑定工作正常。但是 complexType MetaTypeComplexContent 与 complexContent 的绑定没有。为什么? 我收到以下错误:
[ERROR] ct-props-correct.4: Error with type 'MetaTypeComplexContent'. Duplicate attribute usages were specified with the same name and target namespace. The name of the duplicate attribute is 'MyName'.
Line 19 of file: / D: /temp/Stackoverflow/Test.xsd
A schema Could not be parsed.
解决方法
错误信息似乎很清楚地指出了问题:
[错误] ct-props-correct.4:类型为“MetaTypeComplexContent”的错误。使用相同的名称和目标命名空间指定了重复的属性用法。重复属性的名称是“MyName”。
扩展复杂类型时,不应重新声明继承的内容。也许您打算在扩展复杂类型中添加一个新属性,但您忘记更改名称?
要修复错误,请执行以下操作之一:
- 删除复杂类型扩展
- 更改复杂类型扩展中属性的名称或命名空间
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。