問題描述
使用 JAXB 生成 XML 綁定類.
Using JAXB to generate XML binding classes.
架構基于一組舊版 XML 文件,并包含以下代碼段:
The schema is based on a set of legacy XML files, and includes this snippet:
<xs:complexType name="MetaType">
<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>
'Value'屬性與xs:string
的'value'屬性沖突,代碼生成失敗,報錯:
The 'Value' attribute conflicts with the 'value' property of xs:string
, and the code generation fails with the error:
com.sun.istack.SAXParseException2: Property "Value" is already defined. Use <jaxb:property> to resolve this conflict.
推薦答案
答案在于利用 JAXB 綁定(site-template.xjb
):
The answer lies in making use of JAXB bindings (site-template.xjb
):
<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="site-template.xsd" version="1.0">
<!-- Customise the package name -->
<schemaBindings>
<package name="com.example.schema"/>
</schemaBindings>
<!-- rename the value element -->
<bindings node="http://xs:complexType[@name='MetaType']">
<bindings node=".//xs:attribute[@name='Value']">
<property name="ValueAttribute"/>
</bindings>
</bindings>
</bindings>
</bindings>
XPath 表達式定位節點并對其進行重命名,從而避免命名沖突.
The XPath expressions locate the nodes and renames it, thereby avoiding the naming conflict.
使用此綁定 XML 文件,生成的 Java 類最終具有所需的 getValueAttribute()
(以及 getValue()
).
Using this bindings XML file, the generated Java class ends up having the desired getValueAttribute()
(as well as the getValue()
).
這篇關于JAXB - 屬性“值";已經定義了.使用 <jaxb:property>解決這個沖突的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!