在JAR中组织XSD [英] Organizing XSDs in a JAR

查看:86
本文介绍了在JAR中组织XSD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JAR中有以下结构:

I have the following structure in a JAR:


  • Products.xsd

  • ProductCommonTypes。 xsd

  • / com


    • / foo


      • Products.class

      当我这样做时

      schemaURL = Products.class.getClassLoader().getResource(schemaFile);
      

      我看到使用schemaURL:jar:file:/ C:/my.jar!/ Products。 XSD。我认为这很好。

      I see "Using schemaURL: jar:file:/C:/my.jar!/Products.xsd". I think this is good.

      稍后,我尝试创建一个Schema并获得一个异常,说明无法解析名称'common:nonEmptyString'到a( n)'类型定义'组件。

      Later, I try to create a Schema and get an exception stating "Cannot resolve the name 'common:nonEmptyString' to a(n) 'type definition' component."

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

      I believe that the problem is that it is unable to find common:nonEmptyString (which is in ProductCommonTypes.xsd) but can not figure out how to fix it.

      Products.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>
      

      [ jaxb - 无法从包含其他xsd的XSD解组
      建议添加所有模式引用,但我是使用许多模式,所有模式都导入其他几个模式。好像很快就会变丑!

      [jaxb - Unable to unmarshall from XSD which included other xsd suggests adding all schema references, but I am working with many schemas that all import several other schemas. Seems like it could get ugly quick!


      1. 是否有通用/动态解决方案,不需要我识别和硬编码所有我计划每次使用可能导入其他人的模式吗?

      1. Is there a generic/dynamic solution that does not require me to identify and hardcode all schemas everytime I plan on working with one that might import others?

      我计划将所有XSD移动到 MyXSDs 文件夹中罐子在某个时刻。在移动它们之后,由于位置变化,我是否必须做任何不同的事情?

      I plan to move all XSDs to a MyXSDs folder within the jar at some point. After moving them, will I have to do anything differently due to the location change?


      推荐答案

      解决方案

      我能够使用 XMLCatalogResolver 并创建 XML目录文件。

      I was able to solve this using an XMLCatalogResolver and creating an XML catalog file.

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

      I moved all of my XSDs into a single folder and have the following structure in my JAR:


      • MyXSDs


        • Products.xsd

        • ProductCommonTypes.xsd


        • / foo


          • Products.class

          首先,我创建了目录文件并确定了被引用的模式。

          First, I created the catalog file and identified the schema that was being referenced.

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

          然后,我创建了一个 SchemaFactory 。请务必注意,必须根据绝对URI的有序数组 /org/apache/xerces/util/XMLCatalogResolver.html#XMLCatalogResolver(java.lang.String [])rel =nofollow noreferrer>构造函数的文档

          Then, I created an XMLCatalogResolver that uses my catalog file and used it as the ResourceResolver for my SchemaFactory. It is important to note that the catalogs must be specified as an ordered array of absolute URIs as per the constructor's documentation.

          // The catalogs must be an ordered array list of *ABSOLUTE* URIs
          String[] catalogs = new String[] { xmlCatalogAbsPath };
          XMLCatalogResolver resourceResolver = new XMLCatalogResolver(catalogs);
          schemaFactory.setResourceResolver(resourceResolver);
          

          您必须为XSD文件指定相对路径。因此,因为我将我的所有内容放入文件夹,所以我将 relatativeSchemaPath =MyXSDs / Products.xsd设置为与 ClassLoader.getSystemResource

          You must specify the relative path to the XSD file. Therefore, because I put all of mine into a folder, I set relatativeSchemaPath="MyXSDs/Products.xsd" for use with ClassLoader.getSystemResource.

          StreamSource streamSource = new StreamSource(ClassLoader.getSystemResource(relatativeSchemaPath).toString());
          Schema schema = schemaFactory.newSchema(streamSource);
          

          这篇关于在JAR中组织XSD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆