Axis roll call Axes

page 48 self axis Contains the context node itself. The self axis can be abbreviated with a single period . . The expressions self:: and . are equivalent. attribute axis Contains the attributes of the context node. If the context node is not an element node, this axis is empty. The attribute axis can be abbreviated with the at-sign . The expressions attribute::type and type are equivalent. ancestor axis Contains the parent of the context node, the parents parent, etc. The ancestor axis always contains the root node unless the context node is the root node. ancestor-or-self axis Contains the context node, its parent, its parents parent, and so on. This axis always includes the root node. descendant axis Contains all children of the context node, all children of all the children of the context node, and so on. The children are all of the comment, element, processing instruction, and text nodes beneath the context node. In other words, the descendant axis does not include attribute or namespace nodes. As we discussed earlier, although an attribute node has an element node as a parent, an attribute node is not considered a child of that element. descendant-or-self axis Contains the context node and all the children of the context node, all the children of all the children of the context node, all the children of the children of all the children of the context node, and so on. As always, the children of the context node include all comment, element, processing instruction, and text nodes; attribute and namespace nodes are not included. preceding-sibling axis Contains all preceding siblings of the context node; in other words, all nodes that have the same parent as the context node and appear before the context node in the XML document. If the context node is an attribute node or a namespace node, the preceding-sibling axis is empty. following-sibling axis Contains all the following siblings of the context node; in other words, all nodes that have the same parent as the context node and appear after the context node in the XML document. If the context node is an attribute node or a namespace node, the following-sibling axis is empty. preceding axis Contains all nodes that appear before the context node in the document, except ancestors, attribute nodes, and namespace nodes. following axis Contains all nodes that appear after the context node in the document, except descendants, attribute nodes, and namespace nodes. page 49 namespace axis Contains the namespace nodes of the context node. If the context node is not an element node, this axis is empty.

3.2.7 Predicates

Theres one more aspect of XPath expressions that we havent discussed: predicates. Predicates are filters that restrict the nodes selected by an XPath expression. Each predicate is evaluated and converted to a Boolean value either true or false . If the predicate is true for a given node, that node will be selected; otherwise, the node is not selected. Predicates always appear inside square brackets [] . Heres an example: xsl:apply-templates select=line[3] This expression selects the third line element in the current context. If there are two or fewer line elements in the current context, this XPath expression returns an empty node- set. Several things can be part of a predicate; well go through them here.

3.2.7.1 Numbers in predicates

A number inside square brackets selects nodes that have a particular position. For example, the XPath expression line[7] selects the seventh line element in the context node. XPath also provides the boolean and and or operators as well as the union operator | to combine predicates. The expression line[position=3 and style] matches all line elements that occur third and have a style attribute, while line[position=3 or style] matches all line elements that either occur third or have a style attribute. With the union operator, the expression line[3|7] matches all third and seventh line elements in the current context, as does the more verbose line[3] | line[7] .

3.2.7.2 Functions in predicates

In addition to numbers, we can use XPath and XSLT functions inside predicates. Here are some examples: line[last] Selects the last line element in the current context. line[position mod 2 = 0] Selects all even-numbered line elements. The mod operator returns the remainder after a division; the position of any even-numbered element divided by 2 has a remainder of 0. sonnet[type=Shakespearean] Selects all sonnet elements that have a type attribute with the value Shakespearean . ancestor::table[border=1] Selects all table ancestors of the current context that have a border attribute with the value 1 . countbodytable[border=1] Returns the number of table elements with a border attribute equal to 1 that are children of body elements that are children of the root node. Notice that in this case were using an XPath predicate expression as an argument to a function. page 50

3.3 Attribute Value Templates

Although theyre technically defined in the XSLT specification in section 7.6.2, to be exact, well discuss attribute value templates here. An attribute value template is an XPath expression that is evaluated, and the result of that evaluation replaces the attribute value template. For example, we could create an HTML table element like this: table border={size} In this example, the XPath expression size is evaluated, and its value, whatever that happens to be, is inserted into the output tree as the value of the border attribute. Attribute value templates can be used in any literal result elements in your stylesheet for HTML elements and other things that arent part of the XSLT namespace, for example. You can also use attribute value templates in the following XSLT attributes: • The name and namespace attributes of the xsl:attribute element • The name and namespace attributes of the xsl:element element • The format , lang , letter-value , grouping-separator , and grouping-size attributes of the xsl:number element • The name attribute of the xsl:processing-instruction element • The lang , data-type , order , and case-order attributes of the xsl:sort element

3.4 XPath Datatypes

An XPath expression returns one of four datatypes: node-set Represents a set of nodes. The set can be empty, or it can contain any number of nodes. boolean Represents the value true or false . Be aware that the true or false strings have no special meaning or value in XPath; see Section 4.2.1.2 in Chapter 4 for a more detailed discussion of boolean values. number Represents a floating-point number. All numbers in XPath and XSLT are implemented as floating-point numbers; the integer or int datatype does not exist in XPath and XSLT. Specifically, all numbers are implemented as IEEE 754 floating- point numbers, the same standard used by the Java float and double primitive types. In addition to ordinary numbers, there are five special values for numbers: positive and negative infinity, positive and negative zero, and NaN , the special symbol for anything that is not a number. string Represents zero or more characters, as defined in the XML specification. These datatypes are usually simple, and with the exception of node-sets, converting between types is usually straightforward. We wont discuss these datatypes in any more detail here; instead, well discuss datatypes and conversions as we need them to do specific tasks.