Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
There is another workaround to this problem, which is add type conversions which you know are unnecessary, but which placate the type checker. So you could write the above expression instead as:
if (string(@quantity) = “out-of-stock”)
then -2
else if (string(@quantity) = “unknown”)
then -1
else xs:integer(@quantity) + 2
Summary
This chapter provided details of all the type-related constructs and expressions in the XPath 2.0 language.
At the beginning of the chapter we described the
cast
and
castable
operators, and constructor functions, which are used to convert an atomic value of one type to an atomic value of a different type. We provided detailed tables showing all the type conversions that are allowed by the language.
Then, moving beyond atomic types, we examined the syntax for describing sequence types. This syntax is used only in two places in XPath, the
instance
of
and
treat
as
expressions, but XQuery and XSLT users will use the same syntax much more widely, for example, whenever the types of variables or functions are declared.
Finally, we explained how these two expressions,
instance
of
and
treat
as
, actually work.
We've now finished our tour of the XPath language syntax. The next chapter returns to XSLT with an explanation of the syntax used in match patterns, which is based on XPath syntax but is not actually part of the XPath language. Then in Chapter 13 we will look at the built-in function library—this chapter provides an alphabetical listing of all the functions in this library, including all the functions provided by every conformant XPath 2.0 processor, together with some additional functions available when XPath is used within an XSLT stylesheet.
Chapter 12
XSLT Patterns
A pattern is used in XSLT to define a condition that a node must satisfy in order to be selected. The most common use of patterns is in the
match
attribute of
Patterns (sometimes called
match patterns
) are used in just six places in an XSLT stylesheet:
Patterns and Expressions
Most of the patterns found in stylesheets are simple and intuitive. For example:
Pattern | Meaning |
title | Matches any |
chapter/title | Matches any |
speech[speaker = “Hamlet”] | Matches any Hamlet |
section/para[1] | Matches any |
The rules for the more complex patterns, however, are quite technical—so I'm afraid some of the explanations in this chapter are not going to be easy reading.
Patterns are defined in terms of the name, type, and content of a node, and its position relative to other nodes in the tree. To understand how patterns work, you therefore need to understand the tree model (described in Chapter 2) and the different kinds of node.
Patterns look very similar to XPath expressions, and it turns out that they are closely related. However, patterns and expressions are not quite the same thing. In terms of its syntax, every pattern is a valid XPath expression, but not every XPath expression is a valid pattern. It wouldn't make any sense to use the expression
2+2
as a pattern, for example—which nodes would it match?
Expressions are defined in the XPath 2.0 Recommendation, which allows them to be used in contexts other than XSLT stylesheets. For example, XPath expressions are used in the XPointer specification to define hyperlinks between documents, and they are used in some Document Object Model (DOM) implementations as a way for applications to navigate around the DOM data structure. XML Schema 1.1 will allow XPath expressions to be used in a schema to define assertions. Patterns are local to the XSLT Recommendation, and they are found only in stylesheets, though the draft XProc pipeline language also uses them.
It would have been quite possible for XSLT to define both the syntax and the meaning of patterns quite independently of the XPath rules for expressions, but this would risk unnecessary inconsistency. What the XSLT language designers chose to do instead was to define the syntax of patterns in such a way that every pattern was sure to be a valid expression, and then to define the formal meaning of the pattern in terms of the meaning of the expression.
Look at the simplest pattern in the earlier examples,
title
. If
title
is used as an expression, it's an abbreviation for
./child::title
, and it means “select all the
title
as something that matches all