|
BDE 4.14.0 Production release
|
Provide a common reader protocol for parsing and validating XML.
This component represents an abstract class balxml::ValidatingReader - an XML reader that provides data validation against DTD or/and XML Schemas(XSD). The balxml::ValidatingReader inherits from the balxml::Reader interface and therefore fully compliant with it. In addition, balxml::ValidatingReader provides additional methods to control the validation. The enableValidation method specifies what type of validation the reader should perform. Setting validationFlag to false produces a non-validating reader. Setting it to true forces the reader perform the validation of input XML data against XSD schemas.
In validating mode the reader should be able obtain external XSD schemas. balxml::ValidatingReader requires that all schema sources must be represented in the form of bsl::streambuf objects. According to W3C standard an information about external XSD schemas can be defined in three places:
xsi:schemaLocation provides hints from the author to a processor regarding the location of schema documents. The schemaLocation attribute value consists of one or more pairs of URI references, separated by white space. The first member of each pair is a namespace name, and the second member of the pair is a hint describing where to find an appropriate schema document for that namespace. The presence of these hints does not require the processor to obtain or use the cited schema documents, and the processor is free to use other schemas obtained by any suitable means. For example, XercesC has a property XercesSchemaExternalSchemaLocation, that informs parser about available schemas exactly in the same format as the attribute schemaLocation in the document instance.Example:
include element has a required schemaLocation attribute, and it contains a URI reference which must identify a schema document.schemaLocation attributes. If present, the schemaLocation attribute is understood in a way which parallels the interpretation of xsi:schemaLocation in (1). Specifically, it provides a hint from the author to a processor regarding the location of a schema document that the author warrants supplies the required components for the namespace identified by the namespace attribute.For all mentioned cases, having the URI reference which identifies a schema and an optional namespace, the processor(parser) should obtain bsl::streambuf object for the schema. For this purpose balxml::ValidatingReader interface defines the two level schemas resolution process:
balxml::Reader::XmlResolverFunctor).Both the schema cache and resolver should be setup before the method open is called.
balxml::ValidatingReader provides two abstract methods to maintain the schema cache:
addSchema, add a schema to the cacheremoveSchemas, clear the cache and remove all schemasThis component does not provide any functions that present a thread safety issue, since the balxml::Reader class is abstract and cannot be instantiated. There is no guarantee that any specific derived class will provide a thread-safe implementation.
This section illustrates intended use of this component.
In this example, we will create a validating parser that parses and validates document again the schema.
The following string describes an XSD schema for the documents we are going to parse:
The following string describes correct XML for a conforming schema. The top-level element contains one XML namespace attribute, with one embedded entry describing a user:
The following string describes invalid XML. More specifically, the XML document is well-formed, but does not conform to our schema:
Now we define a parse method for parsing an XML document and validating against an XSD schema:
In order to read the XML, we first need to construct a balxml::NamespaceRegistry object, a balxml::PrefixStack object, and a TestReader object, where TestReader is a derived implementation of balxml_validatingreader .
The reader uses a balxml::PrefixStack to manage namespace prefixes so we need to set it before we call open.
Setup validation
Now we call the open method to setup the reader for parsing using the data contained in the in the XML string.
Confirm that the bdem::Reader has opened properly
Do actual document reading
process current node here
Cleanup and close the reader.
The main program parses an XML string using the TestReader
Normal end of data
Parser error - document validation failed