简体   繁体   English

XMLCatalog可以用于模式导入吗?

[英]Can XMLCatalog be used for schema imports?

Can you use XMLCatalog to resolve xsds in schema import statements? 您可以使用XMLCatalog解析模式导入语句中的xsds吗? If so, what is the preferred/best practice? 如果是这样,那么首选/最佳做法是什么? I want to package the xsds in a jar, so using a relative schemaLocation has not worked. 我想在一个jar中打包xsds,所以使用相对schemaLocation不起作用。

So far I am trying to do something like: 到目前为止,我正在尝试做类似的事情:

SchemaFactory factory = SchemaFactory
        .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
XMLCatalogResolver catalogResolver = new XMLCatalogResolver(
        new String[]{"/path/to/catalog.xml"});
factory.setResourceResolver(catalogResolver);

Schema schema = factory.newSchema(new StreamSource(ClassLoader
        .getSystemResourceAsStream("config.xsd")));

Without much luck. 没有太多运气。

At a quick look I see two problems: 快速浏览一下,我看到两个问题:

XMLCatalogResolver catalogResolver = new XMLCatalogResolver(
        new String[]{"catalog.xml"});

If you look at the Javadoc for this method you can read 如果您查看此方法的Javadoc,您可以阅读

catalogs - an ordered array list of absolute URIs catalogs - 绝对URI的有序数组列表

which is not what you use. 这不是你用的。

The second problem is here 第二个问题在这里

Schema schema = factory.newSchema(new StreamSource(ClassLoader
        .getSystemResourceAsStream("config.xsd")));

You do not set the system id for the schema so if you have a relative location for the import then that will be resolved relative to the current directory of your application instead of the directory where you have your schema file. 您没有为架构设置系统ID,因此如果您具有导入的相对位置,那么将相对于应用程序的当前目录而不是您拥有架构文件的目录解析该位置。 You need to either to call setSystemId on the source or pass the system id when you create it: 您需要在源上调用setSystemId或在创建时传递系统ID:

new StreamSource(ClassLoader.getSystemResource("config.xsd").toString())

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

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