使用 javax.xml.validation.Validator 对 XSD 进行 XML 验证:无法解析来自第二个 XSD 的类型 [英] XML validation against XSD with javax.xml.validation.Validator: cannot resolve types come from 2nd XSD

查看:29
本文介绍了使用 javax.xml.validation.Validator 对 XSD 进行 XML 验证:无法解析来自第二个 XSD 的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一种根据 XSD 验证 XML 的方法.就我而言,我有多个 XSD 文件.

I am trying to write a method that validates XML against XSD. In my case, I have multiple XSD files.

当我使用 IntelliJ IDEA 之类的工具从我的主 XSD 生成示例 XML 时,一切看起来都很好:生成的示例 XML 和我预期的一样.所以我认为我的 XSD 文件没问题.

When I use tools like IntelliJ IDEA to generate a sample XML from my main XSD everything looks fine: the sample XML is generated and looks as I expect. So I think that my XSD files are okay.

这是我简化的主要 XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:ds="http://www.w3.org/2000/09/xmldsig#">

    <xsd:import schemaLocation="xmldsig-core-schema.xsd" namespace="http://www.w3.org/2000/09/xmldsig#" />

    <xsd:element name="Message">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="messageId" type="xsd:string" />
                <xsd:element ref="ds:Signature" minOccurs="0" />
            </xsd:sequence>
            <xsd:attribute name="Id" type="xsd:string" use="optional"/>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

我下载了 xmldsig-core-schema.xsd 并将其放在与我的 XSD 文件相同的目录中.

I downloaded and put the xmldsig-core-schema.xsd in the same directory with my XSD files.

我得到的错误:

org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'ds:Signature' to a(n) 'element declaration' component.

Java 代码:

InputStream xsdStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("schema/my.xsd");
// 1. InputStream xsdSignatureStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("schema/xmldsig-core-schema.xsd");

Source[] sources = new StreamSource[1];
sources[0] = new StreamSource(xsdStream);
// 2. sources[1] = new StreamSource(xsdSignatureStream);

SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// 3. schemaFactory.setResourceResolver(new ClasspathResourceResolver());
Schema schema = schemaFactory.newSchema(sources);

Validator validator = schema.newValidator();

String xmlString = "...";
StringReader xmlStringReader = new StringReader(xmlString)
Source source = new StreamSource(xmlStringReader);
validator.validate(source);

所以看起来 javax.xml.validation.Validator 没有看到我的第二个 XSD 文件.因此,我尝试在初始化 javax.xml.validation.Schema 时添加它,但它没有帮助(请参阅注释的第 1 行和第 2 行).

So it seems that the javax.xml.validation.Validator does not see my 2nd XSD file. So I tried to add it when I initialize the javax.xml.validation.Schema but it does not help (see commented lines 1. and 2.).

我还尝试创建并添加一个外部 ResourceResolver(请参阅注释的第 3 行.),但没有帮助.

I also tried to create and add an external ResourceResolver (see commented line 3.), but it does not help.

评论:我使用的流初始化正确,文件路径很好,我可以从流中读取内容并将其记录为字符串.

Comment: The streams I use are initialized properly, the file paths are good, I can read the content from streams and log it as strings.

我在这里想念什么?

推荐答案

我怀疑当使用 getResourceAsStream("schema/my.xsd"); 加载架构文档时,它没有一个已知的基本 URI,因此相对 URI xmldsig-core-schema.xsd 不能正确解析.尝试在您的 StreamSource 对象上设置基本 URI(systemId 属性).

I suspect that when the schema document is loaded using getResourceAsStream("schema/my.xsd"); it doesn't have a known base URI and therefore the relative URI xmldsig-core-schema.xsd doesn't resolve properly. Try setting a base URI (systemId property) on your StreamSource object.

这篇关于使用 javax.xml.validation.Validator 对 XSD 进行 XML 验证:无法解析来自第二个 XSD 的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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