XMLCatalog 可以用于模式导入吗? [英] Can XMLCatalog be used for schema imports?

查看:46
本文介绍了XMLCatalog 可以用于模式导入吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以使用 XMLCatalog 来解析模式导入语句中的 xsds 吗?如果是这样,首选/最佳实践是什么?我想将 xsds 打包在 jar 中,因此使用相对 schemaLocation 不起作用.

Can you use XMLCatalog to resolve xsds in schema import statements? 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.

到目前为止,我正在尝试执行以下操作:

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")));

运气不佳.

推荐答案

快速浏览我发现两个问题:

At a quick look I see two problems:

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

如果你看看 此方法的 Javadoc 您可以阅读

If you look at the Javadoc for this method you can read

catalogs - 一个有序的数组列表绝对 URI

catalogs - an ordered array list of absolute URIs

这不是您使用的.

第二个问题来了

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

您没有为架构设置系统 ID,因此如果您有导入的相对位置,那么它将相对于应用程序的当前目录而不是架构文件所在的目录进行解析.您需要在源上调用 setSystemId 或在创建时传递系统 ID:

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. You need to either to call setSystemId on the source or pass the system id when you create it:

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

这篇关于XMLCatalog 可以用于模式导入吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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