jaxb-无法从包含其他XSD的XSD解组

jaxb-无法从包含其他XSD的XSD解组,jaxb,xsd,Jaxb,Xsd,我有一个XSD(copy metainfo.XSD),其中包括其他XSD(test component types.XSD)。我已经从copy-metainfo生成了工件,但我正在尝试使用jaxb api从copy-metainfo.xsd中解组工件 这是我的xsd test-component-types.xsd <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <xs:schema xmlns

我有一个XSD(copy metainfo.XSD),其中包括其他XSD(test component types.XSD)。我已经从copy-metainfo生成了工件,但我正在尝试使用jaxb api从copy-metainfo.xsd中解组工件

这是我的xsd

test-component-types.xsd

  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>

  <xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">

    <xs:complexType name="testComponent">
        <xs:attribute name="type" type="testComponentType"/>
    </xs:complexType>

    <xs:simpleType name="testComponentType">
      <xs:restriction base="xs:string">
        <xs:enumeration value="TYPE1"/>
        <xs:enumeration value="TYPE2"/>
      </xs:restriction>
    </xs:simpleType>

  </xs:schema>





copy-metainfo.xsd

<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified"
    xmlns:test="http://xmlns.test.com/cie/test/copy-metainfo"
    targetNamespace="http://xmlns.test.com/cie/test/copy-metainfo">

    <xs:include schemaLocation="test-component-types.xsd"/>

    <xs:element name="copy-metainfos" type="test:copy-metainfos-type" />

     <xs:complexType name="copy-metainfos-type">
      <xs:sequence>
      <xs:element name="copy-metainfo" type="test:copy-metainfo" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    </xs:complexType>

     <xs:complexType name="copy-metainfo">
      <xs:sequence>
         <xs:element name="file-paths" type="test:FilePaths" minOccurs="1" maxOccurs="1"/>
      </xs:sequence>
      <xs:attribute name="test-type" type="test:testComponentType"/>
    </xs:complexType>

    <xs:complexType name="FilePaths">
      <xs:sequence>
         <xs:element name="location" type="test:Location" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>    
    </xs:complexType> 

    <xs:complexType name="Location">
        <xs:attribute name="src" type="xs:string"/>
    </xs:complexType>


  </xs:schema>
错误:

org.xml.sax.SAXParseException;行号:22;栏目号:64;src resolve:无法将名称“test:testComponentType”解析为(n)“类型定义”组件

第22行是“”

这是我试图从jar中解压的XML:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<copy-metainfos xmlns="http://xmlns.test.com/cie/test/copy-metainfo">
    <copy-metainfo test-type="TYPE1">
        <file-paths>
            <location src="common"/>
        </file-paths>
    </copy-metainfo>
</copy-metainfos>

我验证了XSD是否在jar中,“COPY_METAINFO_SCHEMA”变量指向正确的位置。如果我在copy-metainfo.xsd中直接指定testComponentType,而不是包括test-component-types.xsd,那么它就可以工作

XSD、XML或java代码有什么问题吗?

XML是正确的(在语义和语法上),我通过

必须将所有XSD添加到模式验证器..test-component-types.XSD和copy-metainfo.XSD

InputStream xsdStream = CopyMetaInfos.class.getClassLoader().getResourceAsStream(COPY_METAINFO_SCHEMA);
这样,您只添加了一个XSD,并且缺少了
test:testComponentType

  JAXBContext jaxbContext = JAXBContext.newInstance(new Class[]{CopyMetaInfos.class});
  SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

  //Source copy-metainfo.xsd
  InputStream xsdStream = CopyMetaInfos.class.getClassLoader().getResourceAsStream(COPY_METAINFO_SCHEMA);
  StreamSource xsdSource = new StreamSource(xsdStream);

  //Source test-component-types.xsd
  InputStream xsdStreamTest = CopyMetaInfos.class.getClassLoader().getResourceAsStream(TEST_COMPONENT_TYPES);
  StreamSource xsdSourceTest = new StreamSource(xsdStreamTest);

  Schema schema = schemaFactory.newSchema(new StreamSource[]{xsdSource,xsdSourceTest});

  Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
  unmarshaller.setSchema(schema);
  return (CopyMetaInfos) unmarshaller.unmarshal(is);
  JAXBContext jaxbContext = JAXBContext.newInstance(new Class[]{CopyMetaInfos.class});
  SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

  //Source copy-metainfo.xsd
  InputStream xsdStream = CopyMetaInfos.class.getClassLoader().getResourceAsStream(COPY_METAINFO_SCHEMA);
  StreamSource xsdSource = new StreamSource(xsdStream);

  //Source test-component-types.xsd
  InputStream xsdStreamTest = CopyMetaInfos.class.getClassLoader().getResourceAsStream(TEST_COMPONENT_TYPES);
  StreamSource xsdSourceTest = new StreamSource(xsdStreamTest);

  Schema schema = schemaFactory.newSchema(new StreamSource[]{xsdSource,xsdSourceTest});

  Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
  unmarshaller.setSchema(schema);
  return (CopyMetaInfos) unmarshaller.unmarshal(is);