Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
These weak conversions are applied to the values of variables and parameters, and they are also used when converting the arguments supplied in a function call to the types declared in the function signature.
Strong conversions can be achieved by use of constructor functions; for example, you can convert a string to an integer with the function
xs:integer($s)
. Generally in XSLT 2.0, strong conversions are not applied automatically; you have to ask for them. Strong conversion is also referred to as
casting
, and the rules for casting are given in Chapter 11.
In the case of function calls, strong conversions are sometimes applied implicitly if you run the stylesheet in backward-compatible mode. This is described in Chapter 3 on page 128. You can select backward-compatible mode by specifying
version=“1.0”
on the
Parameters
Parameters in XSLT are very similar to variables. They are declared with an
As with variables, the expected type of a parameter can be declared using an
as
attribute. Here it's especially useful to declare the expected type, because you can then fail cleanly if the caller supplies the wrong type of value, rather than crashing or producing wrong answers. It's also very valuable to the XSLT compiler to know in advance what type of value will be supplied, because it means that it can generate tighter code that doesn't have to deal with as many runtime possibilities. You don't have to declare the types of parameters, and if you don't, then any type of value will be accepted—but I would recommend as good programming practice always to declare the types. I certainly find that it catches many of my sillier programming mistakes.
Expressions
The syntax of expressions is defined in the XPath 2.0 Recommendation, and is described in detail in Chapters 7 through 11.
XPath expressions are used in a number of contexts in an XSLT stylesheet. They are used as attribute values for many XSLT elements, for example:
In this example
$x
and
$y
are references to variables, and the operators
+
and
*
have their usual meanings of addition and multiplication.
Many XPath expressions, like this one, follow a syntax that is similar to other programming languages. The one that stands out, however, and the one that gave XPath its name, is the
path expression
.
A path expression defines a navigation path through the document tree. Starting at a defined origin, usually either the current node or the document node, it follows a sequence of steps in defined directions. At each stage the path can branch, so for example you can find all the attributes of all the children of the origin node. The result is always a sequence of nodes in a fixed order (known as document order) with no duplicates. It might be empty or contain only one node, but it is still treated as a sequence.