1
我尝试使用给定的XSD语法文件验证XML文件。但现在它总是返回错误,说no declaration found for element ...
和我的XML文件中的每个元素或属性。使用XSD进行Xerces XML验证
要创建XSD,我使用Free online XSD generator,如果我使用同一网站上的(Validator)[http://www.freeformatter.com/xml-validator-xsd.html]检查该XSD中的xml,一切都很好。
那么,为什么Xerces的失败?
我用下面的代码来验证:
XercesDOMParser domParser;
if (domParser.loadGrammar(schemaFilePath.c_str(), Grammar::SchemaGrammarType) == NULL)
{
throw Except("couldn't load schema");
}
ParserErrorHandler parserErrorHandler;
domParser.setErrorHandler(&parserErrorHandler);
domParser.setValidationScheme(XercesDOMParser::Val_Always);
domParser.setDoNamespaces(true);
domParser.setDoSchema(true);
domParser.setValidationSchemaFullChecking(true);
domParser.parse(xmlFilePath.c_str());
if(domParser.getErrorCount() != 0)
{
throw Except("Invalid XML vs. XSD: " + parserErrorHandler.getErrors()); //merge a error coming from my interceptor ....
}
我的XML测试文件是:
<?xml version="1.0" encoding="UTF-8" ?>
<schemes signature="9fadde05">
<!-- NOTE: Do not modify this file.
Any modifications will invalidate the signature and result in an invalid file!
This is an example scheme, param_set etc... can be rename/market or/product
-->
<scheme>
<name>test1</name>
<other>test2</other>
</scheme>
<param_set>
<input>
<height min="1060" max="1100" />
<width min="1900" max="1940" />
</input>
</param_set>
</schemes>
而且我用的XSD是:
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="schemes">
<xs:complexType>
<xs:sequence>
<xs:element name="scheme">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="name"/>
<xs:element type="xs:string" name="other"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="param_set">
<xs:complexType>
<xs:sequence>
<xs:element name="input">
<xs:complexType>
<xs:sequence>
<xs:element name="height">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:short" name="min"/>
<xs:attribute type="xs:short" name="max"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="width">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:short" name="min"/>
<xs:attribute type="xs:short" name="max"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:string" name="signature"/>
</xs:complexType>
</xs:element>
</xs:schema>
简而言之,可能的解决方法是使用DOMLSParser如[链接](http://codesynthesis.com/~boris/data/load-grammar/load-grammar-dom.cxx)和博客[链接](http://www.codesynthesis.com/~boris/blog/2010/03/15/validating-external-schemas-xerces-cxx/)。 但我不知道这个问题的根本原因。 另外,我发现两个可能重复的问题:[链接](http://stackoverflow.com/questions/5870797/xerces-c-xml-schema-validation-not-working)和[链接](HTTP://计算器的.com /问题/ 2455071 /验证文档合的xerces-C)。 –
谢谢,我会尽快查看那些链接。 – alexbuisson
Re-phrase my answer:总之,一种可能的解决方法是使用DOMLSParser,如[本示例程序]中所述(http://codesynthesis.com/~boris/data/load-grammar/load-grammar-dom.cxx)这在[本博客]中有描述(http://www.codesynthesis.com/~boris/blog/2010/03/15/validating-external-schemas-xerces-cxx/)。 但我不知道这个问题的根源。 另外,我发现两种可能的重复的问题:[1](http://stackoverflow.com/questions/5870797/xerces-c-xml-schema-validation-not-working)和[2](HTTP:// stackoverflow.com/questions/2455071/validating-document-in-xerces-c)。 –