Schema-aware XSLT and XQuery from Java and C#
If a query or stylesheet is schema-aware, it will typically expect the principal input document to be schema-validated. There are a number of ways this can be achieved:
The equivalent of the
-valcommand-line option is to set the configuration property SCHEMA_VALIDATION.This option switches validation on for all source documents used by any transformation within this
Configuration. Setting the value tolax, which performs validation only if a schema is available, is sometimes appropriate, but for most applications of any complexity the valuestricttends to result in spurious failures, resulting from attempts to validate documents for which no schema exists.The source document can be parsed and built into a tree before the query or transformation starts, typically by using a
DocumentBuilderset to perform schema validation, or by using aSchemaValidatorwith its destination set to anXdmDestination.Note: building the document this way means that whitespace stripping will not be under the control of
xsl:strip-spaceandxsl:preserve-spacedeclarations in the stylesheet.For any method that expects a
Sourceas input (for example, thetransform()method in an XSLT transformer), it is possible to supply an instance of the class AugmentedSource object. AnAugmentedSourceis a wrapper around a normal JAXPSourceobject, in which additional properties can be set: for example, a property to request validation of the document. TheAugmentedSourceitself implements the JAXPSourceinterface, so it can be used anywhere that an ordinarySourceobject can be used, notably as the first argument to thetransformmethod of theTransformer, and as the return value from a user-writtenURIResolver.If the standard Saxon
URIResolveris used, and recognition of query parameters is enabled, it is also possible to control validation for each source document by means of query parameters in the document URI. For example,document('source.xml?validation=strict')requests the loading of the filesource.xmlwith strict validation.
The configuration property VALIDATION_WARNINGS
has the same effect as the -outval:recover option
on the command line: validation errors encountered when processing the final result tree
are reported to the ErrorListener as warnings, not as fatal errors.
If you are validating the result tree, and you want your application to have access to the
type annotations in the validated tree, then you supply an XdmDestination
as the destination for the query or transformation results. From this you can extract the
XdmNode representing the root of the tree, and thence navigate to other nodes
in the tree. To get the actual type annotation you need to drop into lower-level Saxon
interfaces, by obtaining the underlying NodeInfo object and calling its
getSchemaType() method.