代码之家  ›  专栏  ›  技术社区  ›  Jordi

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

  •  0
  • Jordi  · 技术社区  · 4 年前

    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.
    

    关于如何处理这个问题有什么想法吗?

    0 回复  |  直到 4 年前
        1
  •  0
  •   martijn    4 年前

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

    通过xml模式布局的外观,您还可以更改xml模式。如果添加第三个模式,在其中定义complextype的基

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

    那么它应该可以毫无问题地导入。