B.1 Sorting Out the Acronyms

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:

  • An XML formatting vocabulary

  • An XML transformation language

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.

The XSLT and XPath supported by Office 2003 are strictly limited to version 1.0. As of the time of this writing, XSLT 2.0 and XPath 2.0 are still works in progress.

When Microsoft was first getting started with XML, it released a preliminary version of XSL (before it was re-factored as XSLT) as a part of Internet Explorer 5. Microsoft has deprecated that version of "XSL," and you should avoid it. Any time you see the namespace URI http://www.w3.org/TR/WD-xsl, the developer was using this older version, and you'll have difficulties integrating that code with newer projects. Always check XSLT code and documentation to make sure it uses the correct XSLT namespace URI: http://www.w3.org/1999/XSL/Transform. For more information, see the "Unofficial MSXML XSLT FAQ" at http://www.netcrucible.com/xslt/msxml-faq.htm.

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.


Selects the heading element children of the root article element.


Equivalent to /child::article/child::heading.

/article/para[position( )=1]

Selects the first para element child of the root article element.


Equivalent to /article/para[position( )=1].

/article/para[position( )=last( )]

Selects the last para element child of the root article element.

/article/para[last( )]

Equivalent to /article/para[position( )=last( )].

self::node( )

Selects the context node.


Equivalent to self::node( ).


Selects the order element children of the context node.


Equivalent to ./order.


Selects the price attribute of each order element child of the context node.


Equivalent to order/attribute::price.

order[@price > 30]/shipTo

Selects the shipTo element children of each order element child of the context node whose price attribute's value is greater than 30.

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.

Office 2003 XML
Office 2003 XML
ISBN: 0596005385
EAN: 2147483647
Year: 2003
Pages: 135

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net