我正在使用 Xerces-C++(2.6.1 版)SAX2 解析器来验证 XML,例如下面的文档。(这是RFC 5707中定义的 MSML - 媒体服务器标记语言。)
<?xml version="1.0" encoding="UTF-8"?>
<msml version="1.1">
<createconference name="example">
<audiomix>
<n-loudest n="3"/>
<asn ri="10s"/>
</audiomix>
</createconference>
</msml>
RFC 提供了用于验证 MSML 的 XML 模式,我正在尝试将它们与 Xerces SAX2 解析器结合使用来验证和解析 MSML。解析工作正常,但我没有得到任何验证。我怀疑我的问题可能是因为我尝试验证的 MSML 不包含schemaLocation
属性,但我无法控制我收到的 XML - 我想使用 msml.xsd 强制验证schemaLocation
或noNamespaceSchemaLocation
(或没有)在 XML 中提供。
我的代码类似于以下内容。
SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
// Enable the parser's schema support
parser->setFeature(XMLUni::fgXercesSchema, true);
// Schema validation requires namespace processing to be turned on.
parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
// Define the location of the MSML schema.
XMLCh* schemaLocation = XMLString::transcode("/directory/path/msml-core.xsd");
parser->setProperty(XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation,
schemaLocation);
// MSMLHandler is defined elsewhere and inherits from xercesc/sax2/DefaultHandler
// It overrides startElement and fatalError.
MxMSMLHandler* msmlHandler = new MSMLHandler(xiSessionID, xoMSMLResponse);
parser->setContentHandler((ContentHandler*) msmlHandler);
parser->setErrorHandler((ErrorHandler*) msmlHandler);
// Do the parse
parser->parse(*xmlInputSource);