使用java中的导入对远程XSD(位于服务器上)验证XML(我使用xerces)

时间:2015-08-11 13:01:29

标签: java xml xsd xsd-validation xerces

我有以下XSD名称空间:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<xs:import namespace="http://www.w3.org/TR/REC-html40" schemaLocation="a.xsd"/>
<xs:import namespace="urn:schemas-microsoft-com:office:excel" schemaLocation="b.xsd"/>
<xs:import namespace="urn:schemas-microsoft-com:office:office" schemaLocation="c.xsd"/>

这个XSD位于服务器上,假设它的URL是

http://somehost/xsd/main.xsd

我可以轻松阅读,但我无法读取具有非完全限定名称的导入,我猜他们的基本网址与主XSD相同:

http://somehost/xsd/a.xsd
http://somehost/xsd/b.xsd
http://somehost/xsd/c.xsd

如何针对此类XSD验证xml?

我可能必须使用LSResourceResolver,但我无法正确实现它。请帮忙。

1 个答案:

答案 0 :(得分:0)

在您的示例中,您似乎不需要解析器,但如果您发现自己编写解决方案很困难,您可能会发现使用XML目录重新映射URL更加容易。有关如何使用Xerces和XML Schema文档执行此操作的示例,请参阅here。请注意,Xerces的XMLCatalogResolver是一个LSResourceResolver,因此可以在您计划插入自定义索引时使用。