使用key / keyref身份约束将XML模式编译为Java [英] Compiling XML schema to Java with key/keyref identity constraints
问题描述
假设我有以下XML架构:
Lets say I have the following XML schema:
<xs:schema
xmlns="http://www.example.com/data"
xmlns:data="http://www.example.com/data"
targetNamespace="http://www.example.com/data"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="data">
<xs:complexType>
<xs:all>
<xs:element name="countries">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="country" type="country"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="types">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="type" type="type"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="products">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="product" type="product"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
<xs:key name="countryNameKey">
<xs:selector xpath=".//data:country"/>
<xs:field xpath="@name"/>
</xs:key>
<xs:key name="typeNameKey">
<xs:selector xpath=".//data:type"/>
<xs:field xpath="@name"/>
</xs:key>
<xs:keyref name="countryNameRef" refer="data:countryNameKey">
<xs:selector xpath=".//data:product"/>
<xs:field xpath="@country"/>
</xs:keyref>
<xs:keyref name="typeNameRef" refer="data:typeNameKey">
<xs:selector xpath=".//data:product"/>
<xs:field xpath="@type"/>
</xs:keyref>
<xs:unique name="uniqueProducts">
<xs:selector xpath=".//data:product"/>
<xs:field xpath="@country"/>
<xs:field xpath="@type"/>
</xs:unique>
</xs:element>
<xs:complexType name="country">
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="type">
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="product">
<xs:attribute name="country" type="xs:string" use="required"/>
<xs:attribute name="type" type="xs:string" use="required"/>
</xs:complexType>
</xs:schema>
请原谅我做的例子。
As你可以看到它是表格数据。我定义了一些国家,然后我定义了一些类型的产品。然后我将单个产品定义为来自一个国家的类型,例如来自法国的奶酪。
As you can see it is tabular data. I define some countries, then I define some types of product. I then define individual products as a type from a country, cheese from France for example.
这里要注意的重要一点是我使用键
和 keyref
将所有产品交叉引用回原始国家/类型。
The important thing to note here is that I use key
and keyref
to cross-reference all products back to the original country/type.
所以,我的问题是:
是否可以将此模式编译为可以使用Eclipse Moxy解组并且交叉引用完整的java类?
我知道JAXB 2.0规范不支持key / keyref 。我也知道 Moxy会做什么。 1
I know that the JAXB 2.0 spec does not support key/keyref. I also know that Moxy Does.1
此外我知道Moxy没有Maven插件,无论如何,使用XJC生成的类,只需添加一个 jaxb.properties
文件来指定要使用的JAXB提供程序。 2
Further I know that Moxy doesn't have a Maven plugin and, in any case, uses XJC generated classes and simply adds in a jaxb.properties
file to specify the JAXB provider to use.2
所以我怀疑我的问题的答案是否 ,你必须自己制作课程,但我想在放弃希望之前我会检查。
So I suspect the answer to my question is "no, you have to craft the classes yourself", but I thought I'd check before I abandoned hope.
澄清一下,我的产品
元素当前编译(使用 maven-jaxb2-plugin )
To clarify, My product
element currently compiles (using maven-jaxb2-plugin) to
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "product")
public class Product implements Cloneable, CopyTo, Equals, HashCode, ToString {
@XmlAttribute(name = "country", required = true)
protected String country;
@XmlAttribute(name = "type", required = true)
protected String type;
//getters and setters
}
它引用 String
s而不是 Country
和 Type
对象。
It, references the String
s rather than the Country
and Type
objects.
推荐答案
目前 EclipseLink JAXB(MOXy) 仅扩展XJC工具以添加 jaxb.properties
文件,该文件指示MOXy是 JAXB(JSR-222)提供商。我已输入以下增强功能(目前未预定)以跟踪此请求:
Currently EclipseLink JAXB (MOXy) only extends the XJC tool to add a jaxb.properties
file that indicates that MOXy is the JAXB (JSR-222) provider. I have entered the following enhancement (currently unscheduled) to track this request:
- http://bugs.eclipse.org/411619
这篇关于使用key / keyref身份约束将XML模式编译为Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!