繁体   English   中英

JAXB:两个 xsd 和两个同名的 complextype

[英]JAXB: two xsd with two complextype with the same name

这是我的第一个 xsd ( catalegs-schema.xsd ):

<?xml version="1.0" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="request" type="SearchRequestType"/>

  <xs:complexType name="SearchRequestType">
    <xs:sequence>
      <xs:element name="tableName" type="xs:string" minOccurs="0"/>
      <xs:element name="oid" type="xs:string" minOccurs="0"/>
      <xs:element name="owner" type="OwnerType"/>
      <xs:element name="id" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element name="term" type="xs:string" minOccurs="0"/>
      <xs:element name="referenceDate" type="xs:dateTime" minOccurs="0"/>
      <xs:element name="startIndex" type="xs:long" minOccurs="0"/>
      <xs:element name="pageSize" type="xs:long" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="OwnerType">
    <xs:sequence>
      <xs:element name="ownerType" type="xs:string"/>
      <xs:element name="ownerCode" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
  
</xs:schema>

我的第二个 xsd ( oid-schema.xml ):

<?xml version="1.0" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="request" type="SearchRequestType"/>

  <xs:complexType name="SearchRequestType">
    <xs:sequence>
      <xs:element name="oid" type="xs:string" minOccurs="0"/>
        <xs:element name="referenceDate" type="xs:dateTime" minOccurs="0"/>
      <xs:element name="startIndex" type="xs:long" minOccurs="0"/>
      <xs:element name="pageSize" type="xs:long" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>

如您所见,定义了两个SearchRequestType complexType。

我正在使用这个bindings.xml文件:

<jaxb:bindings 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    
    jaxb:extensionBindingPrefixes="annox xjc"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
    xmlns:annox="http://annox.dev.java.net"
    
    version="2.1">
    
    <jaxb:bindings schemaLocation="oid-schema.xsd">
        <jaxb:schemaBindings>
            <jaxb:package name="cat.catsalut.hes.mpi.hazelcast.loader.domain.serveiterritorial.oid"/>
        </jaxb:schemaBindings>
        <jaxb:bindings node="//xs:complexType[@name='SearchRequestType']">
            <jaxb:class name="attemptdatap"/>
            <annox:annotate>@lombok.Builder</annox:annotate>
            <annox:annotate>@lombok.NoArgsConstructor</annox:annotate>
            <annox:annotate>@lombok.AllArgsConstructor</annox:annotate>
        </jaxb:bindings>
    </jaxb:bindings>
    
    <jaxb:bindings schemaLocation="catalegs-schema.xsd">
        <jaxb:schemaBindings>
            <jaxb:package name="cat.catsalut.hes.mpi.hazelcast.loader.domain.serveiterritorial.catalegs"/>
        </jaxb:schemaBindings>
        <jaxb:bindings node="//xs:complexType[@name='SearchRequestType']">
            <annox:annotate>@lombok.Builder</annox:annotate>
            <annox:annotate>@lombok.NoArgsConstructor</annox:annotate>
            <annox:annotate>@lombok.AllArgsConstructor</annox:annotate>
        </jaxb:bindings>
        <jaxb:bindings node="//xs:complexType[@name='OwnerType']">
            <annox:annotate>@lombok.Builder</annox:annotate>
            <annox:annotate>@lombok.NoArgsConstructor</annox:annotate>
            <annox:annotate>@lombok.AllArgsConstructor</annox:annotate>
        </jaxb:bindings>
    </jaxb:bindings>

</jaxb:bindings>

我收到这条消息:

Error while parsing schema(s).Location [ file:/home/jeusdi/projects/salut/mpi/hes-mpi-hazelcast-loader-service/xsd/servei-territorial/merge/catalegs-schema.xsd{4,56}].
org.xml.sax.SAXParseException: 'request' is already defined

完整输出:

$ ~/.sdkman/candidates/java/8.0.201-oracle/bin/xjc -verbose -xmlschema xsd/servei-territorial/merge/catalegs-schema.xsd -xmlschema xsd/servei-territorial/merge/oid-schema.xsd -b xsd/servei-territorial/merge/bindings.xjb -extension -npa -no-header           
parsing a schema...
[ERROR] Unsupported binding namespace "". Perhaps you meant "http://java.sun.com/xml/ns/jaxb/xjc"?
  line 2 of file:/home/jeusdi/projects/salut/mpi/hes-mpi-hazelcast-loader-service/xsd/servei-territorial/merge/catalegs-schema.xsd

[ERROR] Unsupported binding namespace "http://annox.dev.java.net". Perhaps you meant "http://java.sun.com/xml/ns/jaxb/xjc"?
  line 2 of file:/home/jeusdi/projects/salut/mpi/hes-mpi-hazelcast-loader-service/xsd/servei-territorial/merge/catalegs-schema.xsd

[ERROR] Unsupported binding namespace "http://annox.dev.java.net". Perhaps you meant "http://java.sun.com/xml/ns/jaxb/xjc"?
  line 2 of file:/home/jeusdi/projects/salut/mpi/hes-mpi-hazelcast-loader-service/xsd/servei-territorial/merge/oid-schema.xsd

[ERROR] 'request' is already defined
  line 4 of file:/home/jeusdi/projects/salut/mpi/hes-mpi-hazelcast-loader-service/xsd/servei-territorial/merge/oid-schema.xsd

[ERROR] (related to above error) the first definition appears here
  line 4 of file:/home/jeusdi/projects/salut/mpi/hes-mpi-hazelcast-loader-service/xsd/servei-territorial/merge/catalegs-schema.xsd

[ERROR] 'SearchRequestType' is already defined
  line 15 of file:/home/jeusdi/projects/salut/mpi/hes-mpi-hazelcast-loader-service/xsd/servei-territorial/merge/oid-schema.xsd

[ERROR] (related to above error) the first definition appears here
  line 6 of file:/home/jeusdi/projects/salut/mpi/hes-mpi-hazelcast-loader-service/xsd/servei-territorial/merge/catalegs-schema.xsd

Failed to parse a schema.

关于如何处理这个问题的任何想法?

如果您将具有相同名称且没有唯一命名空间的对象的 xml 模式导入到您的项目中,它们将被添加到相同的项目命名空间中,然后您将拥有重复的对象定义。

这可以通过在 xml 架构中添加 xml 命名空间使它们唯一或将 xml 架构导入项目中的不同命名空间来解决。

通过 xml 架构布局的外观,您还可以更改 xml 架构。 如果您添加第三个架构来定义复杂类型的基础

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
      <xs:complexType name="SearchRequestType">
    <xs:sequence>
      <xs:element name="oid" type="xs:string" minOccurs="0"/>
        <xs:element name="referenceDate" type="xs:dateTime" minOccurs="0"/>
      <xs:element name="startIndex" type="xs:long" minOccurs="0"/>
      <xs:element name="pageSize" type="xs:long" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

然后将此基础包含(并扩展)到您的其他 xml 模式中

<?xml version="1.0" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="srtype.xsd" />
  <xs:element name="request" type="SearchRequestType"/>
</xs:schema>
<?xml version="1.0" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:include schemaLocation="srtype.xsd"/>
    <xs:element name="request" type="extSearchRequestType"/>
    <xs:complexType name="extSearchRequestType">
        <xs:complexContent>
            <xs:extension base="SearchRequestType">
                <xs:sequence>
                    <xs:element name="tableName" type="xs:string" minOccurs="0"/>
                    <xs:element name="owner" type="OwnerType"/>
                    <xs:element name="id" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
                    <xs:element name="term" type="xs:string" minOccurs="0"/>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:complexType name="OwnerType">
        <xs:sequence>
            <xs:element name="ownerType" type="xs:string"/>
            <xs:element name="ownerCode" type="xs:string" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

那么它应该没有问题地导入。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM