【发布时间】:2010-10-23 05:28:38
【问题描述】:
我正在使用 XML 架构文档来验证传入的数据文档,但是架构在运行时编译期间似乎失败了,因为它引用了外部架构的一部分的复杂类型。外部模式在文档顶部的<xs:import> 元素中指定。我曾认为这可能是访问问题,所以我将外部文档的副本移动到 localhost 文件夹。我遇到了同样的错误,所以现在我想知道使用 <xs:import> 元素是否存在某种问题。
架构文档片段如下所示:
<xs:schema targetNamespace="http://www.smpte-ra.org/schemas/429-7/2006/CPL" xmlns:cpl="http://www.smpte-ra.org/schemas/429-7/2006/CPL" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
...
<xs:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="http://localhost/TMSWebServices/XMLSchema/xmldsig-core-schema.xsd"/>
...
<xs:element name="Signer" type="ds:KeyInfoType" minOccurs="0"/>
...
</xs:schema>
我尝试运行的代码非常简单(从http://dotnetslackers.com/Community/blogs/haissam/archive/2008/11/06/validate-xml-against-xsd-xml-schema-using-c.aspx 获得)
string XSDFILEPATH = @"http://localhost/TMSWebServices/XMLSchema/CPL.xsd";
string XMLFILEPATH = @"C:\foo\bar\files\TestCPLs\CPL_930f5e92-be03-440c-a2ff-a13f3f16e1d6.xml";
System.Xml.XmlReaderSettings settings = new System.Xml.XmlReaderSettings();
settings.Schemas.Add(null, XSDFILEPATH);
settings.ValidationType = System.Xml.ValidationType.Schema;
System.Xml.XmlDocument document = new System.Xml.XmlDocument();
document.Load(XMLFILEPATH);
System.Xml.XmlReader rdr = System.Xml.XmlReader.Create(new StringReader(document.InnerXml), settings);
while (rdr.Read())
{
}
一切都很顺利,直到在 while 循环之前实例化 XMLReader 对象的那一行。然后它失败并出现类型未声明的错误。它试图查找的类型 KeyInfoType 是在 import 元素的文档之一中定义的。我已确保命名空间对齐。我想知道命名空间定义中的 # 符号是否引起了问题,但删除它们没有任何效果,它只是改变了错误的样子(即“类型'http://www.w3.org/2000/09/xmldsig:KeyInfoType'未声明。”与“类型'@987654323 @' 未声明。")
我怀疑<xs:import> 元素的处理过程中我遗漏了一些东西。任何建议都非常受欢迎。谢谢!
【问题讨论】:
标签: xml validation import schema document