使用嵌套 XSD 架构的 XML 验证错误 - 未声明类型 [英] XML validation error using nested XSD schema - Type not declared
问题描述
我正在使用嵌套的 XSD 架构来验证 XML 文档.导入的 XSD 使用它们自己的目标命名空间,我可以使用 Liquid XML Studio 验证下面给出的示例 XML.但是当我使用下面的 C# 代码运行验证时,它失败并显示类型声明错误(见下文).我花了很多时间试图弄清楚,但没有运气:
I am using a nested XSD schema to validate an XML document. The imported XSDs use their own target namespaces and I can validate the sample XML given below using Liquid XML Studio. But when I run the validation using my C# code below, it fails with the type declaration error (see below). I have spend alot of time trying to figure out, but no luck:
主 XSD 架构 (DataItem.xsd):
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:DataNumeric="Doc.DataNumeric" xmlns:DataYesNo="Doc.DataYesNo" attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import schemaLocation="DataNumeric.xsd" namespace="Doc.DataNumeric" />
<xs:import schemaLocation="DataYesNo.xsd" namespace="Doc.DataYesNo" />
<xs:complexType name="tDataItem">
<xs:choice>
<xs:element name="DataNumeric" type="DataNumeric:tDataNumeric" />
<xs:element name="DataYesNo" type="DataYesNo:tDataYesNo" />
</xs:choice>
</xs:complexType>
<xs:element name="DataItem" type="tDataItem" />
</xs:schema>
包含的 XSD 架构 (DataNumeric.xsd):
**<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:DataNumeric="Doc.DataNumeric" elementFormDefault="qualified" targetNamespace="Doc.DataNumeric" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="tDataNumeric">
<xs:sequence>
<xs:element name="Answer" type="xs:double" />
</xs:sequence>
</xs:complexType>
<xs:element name="DataNumeric" type="DataNumeric:tDataNumeric" />
</xs:schema>**
XML:
<DataItem>
<DataNumeric xmlns:DataNumeric="Doc.DataNumeric">
<DataNumeric:Answer>37.8</DataNumeric:Answer>
</DataNumeric>
</DataItem>
验证错误:
XmlSchemaValidationException:未声明类型Doc.DataNumeric:tDataNumeric".
XmlSchemaValidationException: Type 'Doc.DataNumeric:tDataNumeric' is not declared.
C# 验证码:
XDocument xDoc = XDocument.Parse(xxxxxxx);
string xsdPath = ConfigUtils.GetXsdPath(XsdSchemaIdentifier.HHDataItem);
FileStream fs = new FileStream(xsdPath, FileMode.Open);
XmlReader reader = XmlReader.Create(fs);
XmlSchemaSet xss = new XmlSchemaSet();
xss.Add("", reader);
fs.Close();
fs.Dispose();
xDoc.Validate(xss, null);
推荐答案
经过令人沮丧的一天半的迭代后,我发现了问题.改变:
I have found the problem after a frustrating day and a half of iterations. Changing:
FileStream fs = new FileStream(xsdPath, FileMode.Open);
XmlReader reader = XmlReader.Create(fs);
XmlSchemaSet xss = new XmlSchemaSet();
xss.Add("", reader);
到
XmlSchemaSet xss = new XmlSchemaSet();
xss.Add("", xsdPath);
解决了这个问题.但是,我仍在寻找 WHY 的答案?两者都是将模式添加到模式集的有效方法(即 2 个重载).
solved the problem. However I am still looking for the answer to WHY? Both are valid ways of adding a schema to the schema set (i.e. 2 overloads).
这篇关于使用嵌套 XSD 架构的 XML 验证错误 - 未声明类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!