XML

XSL Patterns

XML documents represent a tree of nodes. XSL patterns provide a query language for locating nodes in XML documents. After the nodes in the XML document are identified using a pattern, the nodes will be transformed using a template. The XSL patterns we will be using have the same format as the patterns we used with XPath, such as / (child), // (ancestor), .(current node), @ (attribute), and * (wildcard). In addition to the patterns we already mentioned, XSL also has filter operators to manipulate, sort, and filter XML data.

XSL Filter Operators

The filter operators are similar to the SQL Where clause. A filter is evaluated as a Boolean on every node that is within the current context. If the Boolean evaluates to true, the node is included in the returned set; otherwise, it's excluded. Filters are enclosed in brackets. Remember, in our discussion of XPath in Chapter 6, we used the following path to select certain elements:

  /child::customer [attribute::customerID= c1] [position() = 1]

This path selects the first customer element from those customer elements that are siblings to the root element that has an attribute equal to c1. The first filter, [attribute::customerID=c1], filters out all the nodes that do not have a customerID attribute equal to c1. The second filter, [position()=1], filters out all the nodes except the node at position one. With XPath, we could rewrite the previous example as follows:

  /child::customer [@customerID= c1] [1]

We will use this abbreviated form in XSL. There are also some expression operators that we can use when working with XSL. The expression operators are listed in the following table:

XSL Expression Operators

Operator Alternative syntax Description
and $and$ Logical-and, shortcut is &&
or $or$ Logical-or, shortcut is ||
not() $not$ Negation
= $eq$ Equality
$ieq$ Case-insensitive equality
!= $ne$ Not equal
$ine$ Case-insensitive inequality
< $lt$ Less than
$ilt$ Case-insensitive less than
<= $le$ Less than or equal
$ile$ Case-insensitive less than or equal
> $gt$ Greater than
$igt$ Case-insensitive greater than
>= $ge$ Greater than or equal
$ige$ Case-insensitive greater than or equal
$all$ Set operation; returns TRUE if the condition is true for all items in the collection
$any$ Set operation; returns TRUE if the condition is true for any item in the collection
| Set operation; returns the union of two sets of nodes

Using these operators, we can create filter patterns that are based on comparisons. We will do this in an example in the section "XSL Document Elements."