4

Consider an XML file, config.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<Configuration
  xsi:noNamespaceSchemaLocation = "rules.xsd"
  xmlns:xsi                     = "http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xi                      = "http://www.w3.org/2001/XInclude"
>
  <xi:include href="bunnies.xml" />
  <xi:include href="doggies.xml" />
</Configuration>

where rules.xsd is an XML Schema document describing the structure & constraints of config.xml after the XIncludes have been processed.


Using xmllint one is able to perform the XIncludes prior to XML Schema validation from the command line:

xmllint --xinclude --schema rules.xsd config.xml

Invalidate changes to the child bunnies.xml or doggies.xml will be picked up by the xmllint tool.


I am looking to use Xerces-C to apply XIncludes prior to XML Schema in order to determine if the software's configuration is valid prior to using the XML in runtime. The road I am currently on has me looking at Xerces's DOMLSParser.

I have managed to get the parser to perform XML Schema validation - or XIncludes - but when I attempt to configure both behaviors, it appears the XML Schema validation checks are run prior to the XIncludes; which falsely invalidates the document.


Working from the XInclude.cpp sample in Xerces-C the parser is initialized & configured like so:

static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull };
DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(gLS);
DOMLSParser       *parser = ((DOMImplementationLS*)impl)->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
DOMConfiguration  *config = parser->getDomConfig();

config->setParameter(XMLUni::fgDOMNamespaces, true);
config->setParameter(XMLUni::fgXercesSchema, true);
config->setParameter(XMLUni::fgXercesHandleMultipleImports, true);
config->setParameter(XMLUni::fgXercesSchemaFullChecking, true);
config->setParameter(XMLUni::fgXercesDoXInclude, true);

config->setParameter(XMLUni::fgDOMValidate, false);

If fgDOMValidate is enabled - validation fails as it runs prior to XIncludes.
If fgDOMValidate is disabled - validation does not run at all; but XIncludes do!


My question - is there a way to configure a Xerces-C parser to do the above? Or should I be thinking of doing it more directly, say create a parser which will do XIncludes only - stream the output into memory - then create a parser which will handle XML Schemas and pass the included XML into another / or reconfigured parser?

Additionally, if anybody has experience or pointers on learning Xerces-C beyond their API documentation - that would be much appreciated!

John
  • 1,037
  • 11
  • 19

0 Answers0