ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

使用key / keyref标识约束将XML模式编译为Java

2019-10-08 23:03:00  阅读:185  来源: 互联网

标签:java jaxb moxy


假设我有以下XML架构:

<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>

请原谅人为的例子.

如您所见,它是表格数据.我定义了一些国家,然后我定义了一些类型的产品.然后,我将单个产品定义为来自一个国家的类型,例如来自法国的奶酪.

这里需要注意的重要一点是,我使用key和keyref将所有产品交叉引用回原始国家/类型.

所以,我的问题是:

是否可以将此模式编译为可以使用Eclipse Moxy解组并且交叉引用完整的java类?

我知道JAXB 2.0规范做了not support key/keyref.我也知道Moxy Does.1

此外,我知道Moxy没有Maven插件,并且在任何情况下,都使用XJC生成的类,只需添加一个jaxb.properties文件来指定要使用的JAXB提供程序.00003

所以我怀疑我的问题的答案是“不,你必须亲自制作课程”,但我想在放弃希望之前我会检查.

为了澄清,我的产品元素目前正在编译(使用maven-jaxb2-plugin)

@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
}

它引用了Strings而不是Country和Type对象.

解决方法:

目前EclipseLink JAXB (MOXy)仅扩展了XJC工具以添加一个jaxb.properties文件,该文件指示MOXy是JAXB (JSR-222)提供者.我已输入以下增强功能(目前未预定)以跟踪此请求:

> http://bugs.eclipse.org/411619

标签:java,jaxb,moxy
来源: https://codeday.me/bug/20191008/1875082.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有