Hi folks,
I am using Xerces C++ 2.7.0. Mostly, it works great, but I am finding
some unexpected behavior with "xsd:include" and "xsd:import" (where
"xsd" denotes the XML Schema namespace). The "included.xsd" file below
illustrates the problem.
----start of "included.xsd" file----
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://www.lanw.com/namespaces/pub"
xmlns="http://www.lanw.com/namespaces/pub">
<xsd:include schemaLocation = "foo.xsd"/>
</xsd:schema>
----end of "included.xsd" file----
Near the end of this email I provide the relevant piece of code that I
use to parse the file.
If the included file ("foo.xsd") exists and contains a (deliberate) error
then Xerces reports the error. So far, so good.
Problem 1
---------
If the included file ("foo.xsd") does not exist then I would expect
Xerces to report an error, but Xerces just silently ignores the error.
Problem 2
---------
If the included file ("foo.xsd") exists and does not contain any errors
then Xerces parses everything okay. However, the resultant DOM tree does
not contain any nodes from the included file. This surprised me because
Xerces is a Schema-validating parser and "xsd:include" is part of XML
schema so I assumed that Xerces would insert contents of the included
file in the DOM tree.
If I replace the "xsd:include" element with a "xsd:import" element
then I still see problems 1 and 2 above.
I suspect that Problem 1 is a bug in Xerces. However, I am not sure about
Problem 2. Is that a bug? Or am I misunderstanding the division of
responsibilities between Xerces and the application developer? Does the
application developer (that is, me) have to walk the DOM tree to find
all "xsd:include" or "xsd:import" elements and call Xerces to parse each
of the specified files and then somehow merge the multiple parse trees?
If this is the case then I would appreciate some pointers to relevant
parts of the Xerces documentation or demos that illustrate this.
Below is the code (cut-n-pasted from my much longer application) that I
use to parse XML files. Perhaps somebody on this mailing list can spot if
I am neglecting to set a relevant option on the parser.
----start of code to use parse file----
// inputFile = name of input file
// isXsdFile = true if input filename ends with ".xsd"
try {
XMLPlatformUtils::Initialize();
} catch(XMLException & ex) {
cerr << "XMLPlatformUtils::Initialize() failed: "
<< StrX(ex.getMessage())
<< endl;
return false;
}
DomXmlParserErrorHandler err_handler;
parser = new XercesDOMParser();
parser->setDoNamespaces(true);
parser->setCreateEntityReferenceNodes(true);
parser->setErrorHandler(&err_handler);
if (isXsdFile) {
parser->setDoSchema(false);
parser->setValidationSchemaFullChecking(false);
parser->setValidationScheme(XercesDOMParser::Val_Never);
} else {
parser->setDoSchema(true);
parser->setValidationSchemaFullChecking(true);
parser->setValidationScheme(XercesDOMParser::Val_Always);
}
try {
if (isXsdFile) {
parser->loadGrammar(inputFile,
Grammar::SchemaGrammarType, true);
}
parser->parse(inputFile);
} catch(const XMLException & ex) {
cerr << "ERROR: " << nputFile << ": "
<< StrX(ex.getMessage())
<< endl;
delete parser;
return 0;
}
----end of code to use parse file----
Regards,
Ciaran.
--
Ciaran McHale, Ph.D. Email: [EMAIL PROTECTED]
Principal Consultant Tel: +44-(0)7866-416-134 (mobile)
IONA Technologies, UK Tel: +44-(0)118-954-6632 (home office)
Fax: +44-(0)118-954-6767