Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
At times these errors can become frustrating. But remember, every time you get one of these error messages, it tells you about a programming mistake that might otherwise have been much harder to track down. With XSLT 1.0, most programming mistakes don't give you an error message, they simply give you wrong output (or no output at all), and it can be a tortuous process debugging the stylesheet to find out where you went wrong. With XSLT 2.0, if you choose to define data types for your stylesheet functions and templates, you can get error messages that make it much clearer where the mistake lies.
You don't request validation of the input document from within the stylesheet. It's assumed that you will request this as part of the way you invoke the transformation, and details of how you do this will vary from one XSLT processor to another. (With Saxon you can use the
-val:strict
option on the command line; with Altova you need an
xsi:schemaLocation
attribute in the source document.) Within your stylesheet, what you can do is to test whether the input has actually been validated. I often write two template rules in the stylesheet as follows:
Source document is not a validated purchase order
The effect of writing the template rules this way is that if the stylesheet is presented with a document that is not a validated purchase order, it will immediately fail and display an error message, rather than trying to process it and producing garbage output.
Note the carefully chosen phrase
a validated purchase order
. It's not enough to supply an XML document that would be deemed valid if you tried to validate it. To pass this test, the document must already have been through a schema processor, and must have passed validation.
If you prefer, you could code the stylesheet to invoke the validation explicitly, by writing the following:
This defines a variable to hold a copy of the input document. The
type
attribute on the