XJC:从无效方案生成 JAXB 类

Posted

技术标签:

【中文标题】XJC:从无效方案生成 JAXB 类【英文标题】:XJC: generate JAXB classes from invalid scheme 【发布时间】:2021-10-12 00:08:15 【问题描述】:

我想生成一组 OGC IndoorGML XML 方案的 JAXB 注释类 (http://schemas.opengis.net/indoorgml/1.0/indoorgmlcore.xsd)

所以我运行xjc -d scr -p [packagename] -nv [path_to_xsd](-nv 抑制输入方案的严格验证)

但运行此命令会导致以下错误消息:

[ERROR] Property "Rows" is already defined. Use <jaxb:property> to resolve this conflict.
  line 653 of http://schemas.opengis.net/gml/3.2.1/geometryPrimitives.xsd

[ERROR] The following location is relevant to the above error
  line 685 of http://schemas.opengis.net/gml/3.2.1/geometryPrimitives.xsd

[ERROR] Property "Title" is already defined. Use <jaxb:property> to resolve this conflict.
  line 261 of http://www.w3.org/1999/xlink.xsd

[ERROR] The following location is relevant to the above error
  line 246 of http://www.w3.org/1999/xlink.xsd

[ERROR] Property "Title" is already defined. Use <jaxb:property> to resolve this conflict.
  line 232 of http://www.w3.org/1999/xlink.xsd

[ERROR] The following location is relevant to the above error
  line 219 of http://www.w3.org/1999/xlink.xsd

并且没有生成类文件。这很奇怪,因为我希望这些“官方”方案是正确的。

但是,还有什么方法可以生成忽略这些错误的 java 类?

【问题讨论】:

看起来您以某种方式加载了相同的架构两次。如果您获取有效模式的两个副本并将它们组合起来,则结果是由于重复而导致的无效模式。但我不太了解 JAXB,无法为您提供帮助。 @MichaelKay 好的,谢谢。不,问题似乎确实出在方案中(“行”在一个文件中定义了两次,请参阅上述行) 【参考方案1】:

经过反复试验并基于this xjb binding file,此命令行将生成代码

xjc -d src -XautoNameResolution -b indoor.xjb -nv indoorgmlcore.xsd

没有使用包名,添加了-XautoNameResolution,并删除了一些扩展。 xjb 文件中的注释表示添加到原始 xjb 的更改。使用了最新的version of xsd。

indoor.xjb 内容:

<jaxb:bindings version="1.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    jaxb:extensionBindingPrefixes="xjc">
    <!--
    xmlns:wildcard="http://jaxb2-commons.dev.java.net/basic/wildcard"
    xmlns:annox="http://annox.dev.java.net"
    -->

    <jaxb:bindings schemaLocation="http://schemas.opengis.net/gml/3.2.1/gml.xsd" 
        node="/xs:schema">
        <jaxb:schemaBindings>
            <jaxb:package name="net.opengis.gml.v_3_2"/>
        </jaxb:schemaBindings>
    </jaxb:bindings>

    <jaxb:bindings schemaLocation="http://schemas.opengis.net/gml/3.2.1/geometryPrimitives.xsd" node="/xs:schema">
        <jaxb:bindings>
            <jaxb:bindings node="//xs:group[@name='PointGrid']/xs:sequence/xs:element[@name='rows']">
                <!-- ** Backwards incompatible -->
                <jaxb:property name="PointGridRows"/>
            </jaxb:bindings>
        </jaxb:bindings>
    </jaxb:bindings>

<!-- *** 68692723 question -->
    <jaxb:bindings schemaLocation="http://www.w3.org/1999/xlink.xsd" node="/xs:schema">
        <jaxb:bindings>
            <jaxb:bindings node="//xs:element[@name='title']">
                <jaxb:property name="XTitle"/>
            </jaxb:bindings>
        </jaxb:bindings>
    </jaxb:bindings>

    <jaxb:bindings schemaLocation="http://schemas.opengis.net/gml/3.2.1/coordinateOperations.xsd">
        <jaxb:bindings node="//xs:element[@name='parameterValue']">
            <jaxb:factoryMethod name="xparameterValue"/>
        </jaxb:bindings>
        <jaxb:bindings node="//xs:element[@name='operationParameter']">
            <jaxb:factoryMethod name="xoperationParameter"/>
        </jaxb:bindings>
        <jaxb:bindings node="//xs:element[@name='OperationParameter']">
            <jaxb:factoryMethod name="XOperationParameter"/>
        </jaxb:bindings>
    </jaxb:bindings>
<!-- *** 68692723 question END -->

    <jaxb:bindings schemaLocation="http://schemas.opengis.net/gml/3.2.1/grids.xsd" node="/xs:schema">
        <jaxb:bindings>
            <jaxb:bindings node="xs:complexType[@name='GridType']/xs:complexContent/xs:extension/xs:sequence/xs:choice/xs:element[@name='axisLabels']">
                <!-- ** Backwards incompatible -->
                <jaxb:property name="GridAxisLabels"/>
            </jaxb:bindings>
        </jaxb:bindings>
    </jaxb:bindings>
        

    <jaxb:bindings schemaLocation="http://schemas.opengis.net/gml/3.2.1/datums.xsd" node="/xs:schema">
        <jaxb:bindings>
            <jaxb:bindings node="//xs:element[@name='secondDefiningParameter']">
                <jaxb:class name="SecondDefiningParameterPropertyElement"/>
            </jaxb:bindings>
            <jaxb:bindings node="//xs:element[@name='ellipsoid']">
                <jaxb:factoryMethod name="EllipsoidPropertyElement"/>
            </jaxb:bindings>
            <jaxb:bindings node="//xs:element[@name='primeMeridian']">
                <jaxb:factoryMethod name="PrimeMeridianPropertyElement"/>
            </jaxb:bindings>
        </jaxb:bindings>
    </jaxb:bindings>

    <jaxb:bindings schemaLocation="http://schemas.opengis.net/gml/3.2.1/coordinateReferenceSystems.xsd" node="/xs:schema">
        <jaxb:bindings>
            <jaxb:bindings node="//xs:element[@name='userDefinedCS']">
                <jaxb:factoryMethod name="UserDefinedCSPropertyElement"/>
            </jaxb:bindings>
            <jaxb:bindings node="//xs:element[@name='cartesianCS']">
                <jaxb:factoryMethod name="CartesianCSPropertyElement"/>
            </jaxb:bindings>
            <jaxb:bindings node="//xs:element[@name='sphericalCS']">
                <jaxb:factoryMethod name="SphericalCSPropertyElement"/>
            </jaxb:bindings>
            <jaxb:bindings node="//xs:element[@name='polarCS']">
                <jaxb:factoryMethod name="PolarCSPropertyElement"/>
            </jaxb:bindings>
            <jaxb:bindings node="//xs:element[@name='linearCS']">
                <jaxb:factoryMethod name="LinearCSPropertyElement"/>
            </jaxb:bindings>
            <jaxb:bindings node="//xs:element[@name='verticalCS']">
                <jaxb:factoryMethod name="VerticalCSPropertyElement"/>
            </jaxb:bindings>
            <jaxb:bindings node="//xs:element[@name='cylindricalCS']">
                <jaxb:factoryMethod name="CylindricalCSPropertyElement"/>
            </jaxb:bindings>
            <jaxb:bindings node="//xs:element[@name='ellipsoidalCS']">
                <jaxb:factoryMethod name="EllipsoidalCSPropertyElement"/>
            </jaxb:bindings>
            <jaxb:bindings node="//xs:element[@name='affineCS']">
                <jaxb:factoryMethod name="AffineCSPropertyElement"/>
            </jaxb:bindings>
            <jaxb:bindings node="//xs:element[@name='timeCS']">
                <jaxb:factoryMethod name="TimeCSPropertyElement"/>
            </jaxb:bindings>

            <jaxb:bindings node="//xs:element[@name='imageDatum']">
                <jaxb:factoryMethod name="ImageDatumPropertyElement"/>
            </jaxb:bindings>
            <jaxb:bindings node="//xs:element[@name='geodeticDatum']">
                <jaxb:factoryMethod name="GeodeticDatumPropertyElement"/>
            </jaxb:bindings>
            <jaxb:bindings node="//xs:element[@name='temporalDatum']">
                <jaxb:factoryMethod name="TemporalDatumPropertyElement"/>
            </jaxb:bindings>
            <jaxb:bindings node="//xs:element[@name='engineeringDatum']">
                <jaxb:factoryMethod name="EngineeringDatumPropertyElement"/>
            </jaxb:bindings>
            <jaxb:bindings node="//xs:element[@name='verticalDatum']">
                <jaxb:factoryMethod name="VerticalDatumPropertyElement"/>
            </jaxb:bindings>
            <jaxb:bindings node="//xs:element[@name='conversion']">
                <jaxb:factoryMethod name="ConversionPropertyElement"/>
            </jaxb:bindings>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

基于this page修复的错误如下

[错误] 两个声明导致 ObjectFactory 类中的冲突。 http://schemas.opengis.net/gml/3.2.1/coordinateOperations.xsd 的第 243 行

【讨论】:

以上是关于XJC:从无效方案生成 JAXB 类的主要内容,如果未能解决你的问题,请参考以下文章

为 JAXB 生成的类添加前缀

如何指定用于 JAXB xjc 生成的类的 XmlAccessorType

JAXB:如何在XSD中指定attr类型时更改XJC生成的类名?

如何使用 xjc工具 生成java

JDK Tools - xjc: 将 XML Schema 编译成 Java 类

JAXB 映射到 JSON