JAXB编译问题 - [错误]属性"任何"已定义

bey*_*ddc 5 jaxb eclipselink moxy

我正在尝试为xccdf-1.1.4.xsd创建JAXB绑定,这是一个可以从XCCDF Schema Location获取的标准模式

我目前正在使用EclipseLink MOXy作为我的JAXB实现,因为我喜欢它也可以生成JSON绑定.

我修复了几次使用外部绑定XML命中臭名昭着的"[ERROR]属性"值"已定义"错误,现在我遇到了错误

[ERROR] Property "Any" is already defined. Use <jaxb:property> to resolve this conflict.
line 441 of file:/home/dchu/Playground/Java/eclipselink_moxy/xccdf_1.1.4/xccdf-1.1.4.xsd

[ERROR] The following location is relevant to the above error
line 444 of file:/home/dchu/Playground/Java/eclipselink_moxy/xccdf_1.1.4/xccdf-1.1.4.xs
Run Code Online (Sandbox Code Playgroud)

下面是XML架构中发生错误的行的片段.

<xsd:sequence>
    <xsd:choice minOccurs="1" maxOccurs="1">
      <xsd:any namespace="http://purl.org/dc/elements/1.1/"
               minOccurs="1" maxOccurs="unbounded"/>
      <xsd:any namespace="http://checklists.nist.gov/sccf/0.1"
               processContents="skip" 
               minOccurs="1" maxOccurs="unbounded"/>
    </xsd:choice>
</xsd:sequence>
Run Code Online (Sandbox Code Playgroud)

有谁知道这里有什么不对吗?谢谢!

bdo*_*han 14

您可以使用外部绑定文件重命名任何属性之一.

binding.xml

<jxb:bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.1">

    <jxb:bindings schemaLocation="schema.xsd">
        <jxb:bindings
            node="//xsd:complexType[@name='foo']/xsd:sequence/xsd:choice/xsd:any[@namespace='http://checklists.nist.gov/sccf/0.1']">
            <jxb:property name="any2" />
        </jxb:bindings>
    </jxb:bindings>

</jxb:bindings>
Run Code Online (Sandbox Code Playgroud)

XML Schema(schema.xsd)

以下是XML架构的简化版本:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.org/schema" 
    xmlns="http://www.example.org/schema"
    elementFormDefault="qualified">

    <xsd:complexType name="foo">
        <xsd:sequence>
            <xsd:choice minOccurs="1" maxOccurs="1">
                <xsd:any namespace=""
                    minOccurs="1" maxOccurs="unbounded" />
                <xsd:any namespace="http://checklists.nist.gov/sccf/0.1"
                    processContents="skip" minOccurs="1" maxOccurs="unbounded" />
            </xsd:choice>
        </xsd:sequence>
    </xsd:complexType>

</xsd:schema>
Run Code Online (Sandbox Code Playgroud)

XJC电话

下面是如何进行利用外部绑定文件的XJC调用.

xjc -b binding.xml schema.xsd
Run Code Online (Sandbox Code Playgroud)

生成的类(Foo)

package org.example.schema;

import java.util.*;
import javax.xml.bind.annotation.*;
import org.w3c.dom.Element;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "foo", propOrder = {
    "any",
    "any2"
})
public class Foo {

    @XmlAnyElement(lax = true)
    protected List<Object> any;
    @XmlAnyElement
    protected List<Element> any2;


    public List<Object> getAny() {
        if (any == null) {
            any = new ArrayList<Object>();
        }
        return this.any;
    }

    public List<Element> getAny2() {
        if (any2 == null) {
            any2 = new ArrayList<Element>();
        }
        return this.any2;
    }

}
Run Code Online (Sandbox Code Playgroud)