When learning XSLT, there are three primary specifications to be aware of: XSL (sometimes referred to as XSL-FO), XSLT, and XPath. Originally, these three languages were just parts of a single language, XSL, or "Extensible Stylesheet Language". But before being released as W3C recommendations, they were re-factored into three separate specifications. They essentially have a subset relationship, as depicted in Figure B-1. XPath is used by XSLT, which is in turn used by XSL.
Figure B-1. The subset relationship between XSL, XSLT, and XPath
This appendix is concerned only with the inner two circles in Figure B-1, XSLT and XPath. We'll see how XSLT and XPath relate to each other and what they actually look like in the tutorial later on. First, let's briefly look at the role each language plays.
B.1.1 What Is XSL?
As we've seen, XSL stands for "Extensible Stylesheet Language." It is a language for expressing stylesheets for XML. It consists of two primary parts:
This appendix is concerned only with XSLT, which is the transformation component of XSL. The other component the formatting vocabulary is commonly called XSL Formatting Objects, or XSL-FO. It theoretically can function apart from XSLT as a standalone formatting vocabulary, but it is usually used as a part of XSL (i.e., with XSLT). The most common use case for XSL-FO is transforming XML documents into documents suitable for printing, particularly in PDF format.
The XSL 1.0 recommendation is located at http://www.w3.org/TR/xsl.
B.1.2 What Is XSLT?
XSLT stands for "Extensible Stylesheet Language Transformations." It is a language for transforming XML documents into other XML documents or other formats, such as HTML and plain text. An XSLT stylesheet is a program that declaratively defines the transformation from a source tree (input) to a result tree (output). Since XSLT stylesheets themselves are represented in XML format, that means there are three essential XML documents, or "trees," involved in any XSLT transformation. Figure B-2 shows a diagram depicting the relationships of these three trees.
Figure B-2. The three trees involved in every XSLT transformation
The input to the XSLT processor (depicted by the rectangle in Figure B-2) consists of the source XML document (the source tree) and the XSLT program itself (the stylesheet tree). The output of the transformation is a new XML document (the result tree).
The XSLT 1.0 recommendation is located at http://www.w3.org/TR/xslt.
B.1.3 What Is XPath?
XPath is short for "XML Path Language." It is an expression language for addressing parts of an XML document. XPath is an essential part of XSLT and is used to select "nodes" in the XML source tree for further processing.
The XPath "data model" is fundamental to XSLT. Mathematically speaking, it defines the entire domain and range of XSLT "functions" in other words, the input to and output from XSLT stylesheets. It defines what a "tree" is, the seven kinds of nodes that can occur in a tree (root, element, attribute, comment, processing instruction, namespace, and text nodes), and how they relate to XML.
Before we get into some XSLT examples, let's take a look at some example XPath expressions. Each of the following XPath expressions is called a location path and returns an object called a node-set. Rather than precisely defining its behavior, we'll provide a description of what each expression selects. In that way, you can begin to learn some of the XPath language by example.
These examples only illustrate a few of XPath's operators and functions. Aside from location paths, XPath also supports the operators that you'd expect to find in a programming language, such as arithmetic (+, -, *, div, and mod) and logic (and, or). You can use XPath to do math and to manipulate strings, as well as to select nodes. For a more thorough investigation (as well as an explanation of how exactly such expressions are evaluated), see one of the books cited at the beginning of this appendix. We'll come across a few more XPath expressions in this appendix (in the context of XSLT).
XPath expressions appear as the values of various attributes in XSLT. For example, the select attribute of a number of XSLT instructions (including the xsl:value-of, xsl:for-each, and xsl:apply-templates elements) contains an XPath expression.
The XPath 1.0 recommendation is located at http://www.w3.org/TR/xpath.