I'm using an XML schema document to validate incoming data documents, however the schema appears be failing during compilation at run time because it refers to a complex type which part of an external schema. The external schema is specified in a <xs:import>
element at the top of the document. I had thought it might be an access problem, so I moved a copy of the external document to a localhost folder. I get the same error, so now I'm wondering if there might be some sort of issue with the use of the <xs:import>
element.
我正在使用XML模式文档来验证传入的数据文档,但是在运行时编译期间模式似乎失败,因为它指的是复杂类型,它是外部模式的一部分。外部模式在文档顶部的
The schema document fragment looks like this:
架构文档片段如下所示:
<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>
The code I'm trying to run this with is real simple (got it from http://dotnetslackers.com/Community/blogs/haissam/archive/2008/11/06/validate-xml-against-xsd-xml-schema-using-c.aspx)
我试图运行它的代码非常简单(来自http://dotnetslackers.com/Community/blogs/haissam/archive/2008/11/06/validate-xml-against-xsd-xml-schema - 使用 - 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())
{
}
Everything goes well until the line that instantiates the XMLReader object just before the while loop. Then it fails with a type not declared error. The type that it's trying to find, KeyInfoType, is defined in one of the the documents in the import element. I've made sure the namespaces line up. I wondered if the # signs in the namespace definitions were causing a problem, but removing them had no effect, it just changed what the error looked like (i.e. "Type 'http://www.w3.org/2000/09/xmldsig:KeyInfoType' is not declared." versus "Type 'http://www.w3.org/2000/09/xmldsig#:KeyInfoType' is not declared.")
一切顺利,直到在while循环之前实例化XMLReader对象的行。然后它失败,类型未声明错误。它试图找到的类型KeyInfoType在import元素的其中一个文档中定义。我确保命名空间排成一行。我想知道名称空间定义中的#符号是否导致问题,但删除它们没有任何效果,它只是改变了错误的样子(即“Type”http://www.w3.org/2000/09/xmldsig :KeyInfoType'未声明。“与”类型'http://www.w3.org/2000/09/xmldsig#:KeyInfoType'未声明。“)
My suspicion is that there's something about the processing of the <xs:import>
element that I'm missing. Any suggestions are very welcome. Thanks!
我怀疑是否存在我缺少的
2 个解决方案
#1
I think you need to add just one line of code to make it work:
我认为你需要添加一行代码才能使它工作:
settings.ValidationFlags =
System.Xml.Schema.XmlSchemaValidationFlags.ProcessSchemaLocation;
Marc
#2
Ok, this is getting a bit baffling. I've tried to do this a few different ways including adding the line:
好吧,这有点令人费解。我尝试过几种不同的方式,包括添加行:
settings.ValidationFlags |= System.Xml.Schema.XmlSchemaValidationFlags.ProcessSchemaLocation;
and I keep getting the same error:
我一直得到同样的错误:
Type 'http://www.w3.org/2000/09/xmldsig#:KeyInfoType' is not declared.
The document specified by that namespace is:
该命名空间指定的文档是:
http://localhost/TMSWebServices/XMLSchema/xmldsig-core-schema.xsd
The document is accessible from where I am and I can locate the (seemingly) offending KeyInfoType type at line 152.
可以从我所在的位置访问该文档,并且我可以在第152行找到(看似)违规的KeyInfoType类型。
Just for fun, I examined the document being validated and found that the element defined in the schema of this type is not located in the document. The schema defines it as optional (minOccurs="0"), so that's not the issue.
为了好玩,我检查了正在验证的文档,发现此类模式中定义的元素不在文档中。模式将其定义为可选(minOccurs =“0”),因此这不是问题。
It's almost as though there's something weird about the framework's ability to compile a schmea document when that document imports external schema documents. Has anyone seen this behavior? Google has not proven fruitful for this problem, although it did give me a bunch of suggestions to try. Thanks!
当文档导入外部模式文档时,几乎就像框架编译schmea文档的能力有些奇怪。有没有人见过这种行为?谷歌没有证明这个问题很有成效,虽然它确实给了我一些尝试的建议。谢谢!
#1
I think you need to add just one line of code to make it work:
我认为你需要添加一行代码才能使它工作:
settings.ValidationFlags =
System.Xml.Schema.XmlSchemaValidationFlags.ProcessSchemaLocation;
Marc
#2
Ok, this is getting a bit baffling. I've tried to do this a few different ways including adding the line:
好吧,这有点令人费解。我尝试过几种不同的方式,包括添加行:
settings.ValidationFlags |= System.Xml.Schema.XmlSchemaValidationFlags.ProcessSchemaLocation;
and I keep getting the same error:
我一直得到同样的错误:
Type 'http://www.w3.org/2000/09/xmldsig#:KeyInfoType' is not declared.
The document specified by that namespace is:
该命名空间指定的文档是:
http://localhost/TMSWebServices/XMLSchema/xmldsig-core-schema.xsd
The document is accessible from where I am and I can locate the (seemingly) offending KeyInfoType type at line 152.
可以从我所在的位置访问该文档,并且我可以在第152行找到(看似)违规的KeyInfoType类型。
Just for fun, I examined the document being validated and found that the element defined in the schema of this type is not located in the document. The schema defines it as optional (minOccurs="0"), so that's not the issue.
为了好玩,我检查了正在验证的文档,发现此类模式中定义的元素不在文档中。模式将其定义为可选(minOccurs =“0”),因此这不是问题。
It's almost as though there's something weird about the framework's ability to compile a schmea document when that document imports external schema documents. Has anyone seen this behavior? Google has not proven fruitful for this problem, although it did give me a bunch of suggestions to try. Thanks!
当文档导入外部模式文档时,几乎就像框架编译schmea文档的能力有些奇怪。有没有人见过这种行为?谷歌没有证明这个问题很有成效,虽然它确实给了我一些尝试的建议。谢谢!