XML 使用 javax.xml.validation.Validator 针对 XSD 进行验证:无法解析来自第二个 XSD 的类型
XML validation against XSD with javax.xml.validation.Validator: cannot resolve types come from 2nd XSD
我正在尝试编写一个方法来验证 XML 与 XSD 的对比。
就我而言,我有多个 XSD 个文件。
当我使用像 IntelliJ IDEA 这样的工具从我的主 XSD 生成样本 XML 时,一切看起来都很好:样本 XML 已生成并且看起来如我所料。所以我认为我的 XSD 文件没问题。
这是我的简化主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 文件放在同一目录中。
我得到的错误:
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.)。
我也尝试创建并添加一个外部 ResourceResolver
(参见第 3 行的注释),但没有帮助。
评论:
我使用的流已正确初始化,文件路径良好,我可以从流中读取内容并将其记录为字符串。
我在这里想念什么?
我怀疑当使用 getResourceAsStream("schema/my.xsd");
加载架构文档时,它没有已知的基本 URI,因此相对 URI xmldsig-core-schema.xsd
无法正确解析。尝试在您的 StreamSource
对象上设置基本 URI (systemId
属性)。
我有同样的错误:
org.xml.sax.SAXParseException: src-resolve: 无法将名称 'ds:Signature' 解析为 (n) 'element declaration' 组件。<code></p>
<p>并浪费了几个小时试图以某种方式处理它。
尝试删除“xmldsig-core-schema.xsd”之间的所有内容:</p>
<pre><?xml version="1.0" encoding="utf-8"?>
和
<schema
它是那个地方的 DOCTYPE 定义和我的假设,解析器无法正确处理它。
我正在尝试编写一个方法来验证 XML 与 XSD 的对比。 就我而言,我有多个 XSD 个文件。
当我使用像 IntelliJ IDEA 这样的工具从我的主 XSD 生成样本 XML 时,一切看起来都很好:样本 XML 已生成并且看起来如我所料。所以我认为我的 XSD 文件没问题。
这是我的简化主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 文件放在同一目录中。
我得到的错误:
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.)。
我也尝试创建并添加一个外部 ResourceResolver
(参见第 3 行的注释),但没有帮助。
评论: 我使用的流已正确初始化,文件路径良好,我可以从流中读取内容并将其记录为字符串。
我在这里想念什么?
我怀疑当使用 getResourceAsStream("schema/my.xsd");
加载架构文档时,它没有已知的基本 URI,因此相对 URI xmldsig-core-schema.xsd
无法正确解析。尝试在您的 StreamSource
对象上设置基本 URI (systemId
属性)。
我有同样的错误:
org.xml.sax.SAXParseException: src-resolve: 无法将名称 'ds:Signature' 解析为 (n) 'element declaration' 组件。<code></p>
<p>并浪费了几个小时试图以某种方式处理它。
尝试删除“xmldsig-core-schema.xsd”之间的所有内容:</p>
<pre><?xml version="1.0" encoding="utf-8"?>
和
<schema
它是那个地方的 DOCTYPE 定义和我的假设,解析器无法正确处理它。