Here is an example how to turn on schema processing in DOMParser (default is off). Note that you must also turn on namespace support (default is off) for schema processing.
// Instantiate the DOM parser.
DOMParser parser;
parser.setDoNamespaces(true);
parser.setDoSchema(true);
parser.parse(xmlFile);
Usage in SAXParser is similar, please refer to the sample program 'samples/SAXCount/SAXCount.cpp' for further reference.
Here is an example how to turn on schema processing in SAX2XMLReader (default is on). Note that namespace must be on (default is on) as well.
Review the sample file, 'samples/data/personal-schema.xml' and 'samples/data/personal.xsd' for an example of an XML Schema grammar.
Associating Schema Grammar with instance document
Schema grammars can be associated with instance documents in two ways.
Specifying Schema Grammar through method calls:
An application developer may associate schemas with instance documents through methodssetExternalSchemaLocation if they use namespaces, andsetExternalNoNamespaceSchemaLocation otherwise. (For SAX2XMLReader, use the properties: "http://apache.org/xml/properties/schema/external-schemaLocation" and "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation")
Here is an example with no target namespace:
// Instantiate the DOM parser.
DOMParser parser;
parser.setDoNamespaces(true);
parser.setDoSchema(true);
parser.setExternalNoNamespaceSchemaLocation("personal.xsd");
parser.parse("test.xml");
// Instantiate the SAX2 XMLReader.
SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
XMLCh* propertyValue = XMLString::transcode("personal.xsd");
ArrayJanitor<XMLCh> janValue(propertyValue);
parser->setProperty(
XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation,
propertyValue);
parser.parse("test.xml");
Here is an example with a target namespace. Note that it is an error to specify a different namespace in setExternalSchemaLocation than the target namespace defined in the Schema.
Specifying Schema Grammar through attributes in the instance document:
If schema grammar was not specified externally through methods, then each instance document that uses XML Schema grammars must specify the location of the grammars it uses by using an xsi:schemaLocation attribute if they use namespaces, and xsi:noNamespaceSchemaLocation attribute otherwise.
Here is an example with a target namespace. Note that it is an error to specify a different namespace in xsi:schemaLocation attribute than the target namespace defined in the Schema.