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 to
lax, which performs validation only if a schema is available, is sometimes appropriate, but for most applications of any complexity the value
stricttends 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 a
SchemaValidatorwith its destination set to an
Note: building the document this way means that whitespace stripping will not be under the control of
xsl:preserve-spacedeclarations in the stylesheet.
For any method that expects a
Sourceas input (for example, the
transform()method in an XSLT transformer), it is possible to supply an instance of the class AugmentedSource object. An
AugmentedSourceis a wrapper around a normal JAXP
Sourceobject, in which additional properties can be set: for example, a property to request validation of the document. The
AugmentedSourceitself implements the JAXP
Sourceinterface, so it can be used anywhere that an ordinary
Sourceobject can be used, notably as the first argument to the
transformmethod of the
Transformer, and as the return value from a user-written
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 file
source.xmlwith strict validation.
The configuration property VALIDATION_WARNINGS
has the same effect as the
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
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