Java 在JAR中组织xsd

Java 在JAR中组织xsd,java,xsd,jaxb,Java,Xsd,Jaxb,我在罐子中有以下结构: Products.xsd ProductCommonTypes.xsd /com /福 产品类别 当我这样做的时候 schemaURL = Products.class.getClassLoader().getResource(schemaFile); 我看到“使用schemaURL:jar:file:/C:/my.jar!/Products.xsd”。我觉得这很好 稍后,我尝试创建一个模式,并得到一个异常,声明“无法将名称'common:nonemptys

我在罐子中有以下结构:

  • Products.xsd
  • ProductCommonTypes.xsd
  • /com
    • /福
      • 产品类别
当我这样做的时候

schemaURL = Products.class.getClassLoader().getResource(schemaFile);
我看到“使用schemaURL:jar:file:/C:/my.jar!/Products.xsd”。我觉得这很好

稍后,我尝试创建一个模式,并得到一个异常,声明“无法将名称'common:nonemptysting'解析为(n)'type definition'组件。”

我认为问题在于它无法找到common:nonEmptyString(在ProductCommonTypes.xsd中),但无法找到如何修复它

产品.xsd

  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
   xmlns:p="http://www.foo.com/Products"
   targetNamespace="http://www.foo.com/Products"
   xmlns:common="http://www.foo.com/ProductsCommonTypes"
   elementFormDefault="qualified">

   <xs:import schemaLocation="ProductsCommonTypes.xsd" 
      namespace="http://www.foo.com/ProductsCommonTypes"/>

   <xs:element name="products">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="name" type="common:nonEmptyString" minOccurs="1" maxOccurs="unbounded"/>
         </xs:sequence>
      </xs:complexType>
   </xs:element>

</xs:schema>

ProductCommonTypes.xsd

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

   <xs:simpleType name="nonEmptyString">
      <xs:annotation>
         <xs:documentation>A type that will require a non-empty string value be present
         </xs:documentation>
      </xs:annotation>
      <xs:restriction base="xs:string">
         <xs:pattern value="(\s*[^\s]\s*)+"></xs:pattern>
      </xs:restriction>
   </xs:simpleType>

   <xs:simpleType name="url">
      <xs:annotation>
         <xs:documentation>A HTTP URL</xs:documentation>
      </xs:annotation>
      <xs:restriction base="xs:anyURI">
         <xs:pattern value="https?://.+"></xs:pattern>
      </xs:restriction>
   </xs:simpleType>
</xs:schema>

需要存在非空字符串值的类型
HTTP URL
[ 建议添加所有模式引用,但我正在使用许多模式,这些模式都会导入其他几个模式。看起来它可能会很快变得丑陋

  • 是否有一种通用/动态解决方案,它不要求我在每次计划使用可能导入其他模式的模式时识别并硬编码所有模式

  • 我计划在某个时候将所有XSD移动到jar中的一个MyXSD文件夹中。移动它们之后,由于位置的改变,我是否需要做任何不同的事情


  • 解决方案

    我能够用一个简单的方法解决这个问题,并创建了一个文件

    我将所有XSD移动到一个文件夹中,JAR中的结构如下:

    • MyXSDs
      • Products.xsd
      • ProductCommonTypes.xsd
    • /com
      • /福
        • 产品类别
    首先,我创建了目录文件并标识了被引用的模式

    <!DOCTYPE catalog
        PUBLIC "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN"
               "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd">
        <catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
    
        <public
            publicId="http://www.foo.com/ProductCommonTypes"
            uri="ProductCommonTypes.xsd"/>
    
    </catalog>
    
    您必须指定XSD文件的相对路径。因此,由于我将所有文件都放在一个文件夹中,因此我将relatativeSchemaPath=“MyXSDs/Products.XSD”设置为与一起使用


    解决方案

    我能够用一个简单的方法解决这个问题,并创建了一个文件

    我将所有XSD移动到一个文件夹中,JAR中的结构如下:

    • MyXSDs
      • Products.xsd
      • ProductCommonTypes.xsd
    • /com
      • /福
        • 产品类别
    首先,我创建了目录文件并标识了被引用的模式

    <!DOCTYPE catalog
        PUBLIC "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN"
               "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd">
        <catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
    
        <public
            publicId="http://www.foo.com/ProductCommonTypes"
            uri="ProductCommonTypes.xsd"/>
    
    </catalog>
    
    您必须指定XSD文件的相对路径。因此,由于我将所有文件都放在一个文件夹中,因此我将relatativeSchemaPath=“MyXSDs/Products.XSD”设置为与一起使用


    我建议您将解决方案从“问题”移动到“答案”以减少混淆。我建议您将解决方案从“问题”移动到“答案”以减少混淆。
    StreamSource streamSource = new StreamSource(ClassLoader.getSystemResource(relatativeSchemaPath).toString());
    Schema schema = schemaFactory.newSchema(streamSource);