我试图指定一个联合模式类型,允许字符串值或对现有元素的引用,以便在Jaxb中使用。这是一个最小的示例架构:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="property" >
<xsd:complexType>
<xsd:attribute name="id" type="xsd:ID" />
<xsd:attribute name="value" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:complexType name="customerType">
<xsd:attribute name="property" type="propType" />
</xsd:complexType>
<xsd:simpleType name="propType">
<xsd:union memberTypes="xsd:IDREF xsd:string" />
</xsd:simpleType>
<xsd:element name="customer" type="customerType" />
</xsd:schema>
&#13;
这里property属性可以直接包含属性值,也可以引用xml文档中定义的属性元素。
由于联合类型自动映射到java-Strings,我使用XmlConversion-Annotation(来自此示例:EclipseLink Example)来提供到允许的模式类型的映射。这适用于允许的类型,即十进制和字符串值,来自示例:
public class CustomerCustomizer implements DescriptorCustomizer {
@Override
public void customize(ClassDescriptor descriptor) throws Exception {
XMLDirectMapping attributeMapping = new XMLDirectMapping();
...
XMLUnionField unionField = new XMLUnionField();
unionField.addSchemaType(XMLConstants.DECIMAL_QNAME);
unionField.addSchemaType(XMLConstants.STRING_QNAME);
...
}
}
&#13;
我使用EclipseLink Moxy 2.5.2测试了这个例子。
不幸的是,我无法找到一种方法来提供在转换中指定引用的功能,即查看属性属性是否指定xml文档中的有效和当前ID,如果为true,则使用这是财产价值。如果没有,请直接使用给定的字符串。
有没有办法在EclipseLink Moxy的联合中使用ID-References? 或者任何其他建议如何实现具有引用(如果在文档中可用)或属性的字符串值的期望行为?
感谢您的帮助!