XSLT, XPath, and XSL Formatting Objects

The main difficulty in working with a new and evolving technology such as XML is keeping up with the many changes being made to the specifications. For example, XSL is now being split into three different specifications: XSLT, XPath, and XSL Formatting Objects (XSL FO). XPath originated from XSL; therefore, as you saw earlier in this chapter, XSL uses essentially the same syntax as XPath. Because we've discussed XPath in detail in Chapter 6, we won't repeat the discussion here. The formatting object specification (the fo namespace) defines a set of formatting semantics developed as an XML vocabulary for working with documents such as PDFs.

  • XSLT is a language for transforming an XML document to any other text-based document, such as an XHTML document. You can use XSLT to define templates for your output so that your XML data can be delivered to the templates. However, the existing Internet Explorer 5 XSL style sheets are not compliant with the final W3C XSLT recommendation. XSLT introduced many new features after Internet Explorer 5 was released. Therefore, we must update the style sheets to XSLT conformance to utilize these new features. Before we discuss how to update the XSL style sheets, let's take a look at the elements in XSLT.

XSLT Elements

XSLT uses several of the elements that we used with XSL in the same manner. For example, the following elements have not changed: apply-templates, attribute, element, comment, copy, for-each, if, choose, when, and otherwise. The pi element used in XSL is called processing-instruction in XSLT.

The following elements that exist in XSL do not exist in XSLT: script, cdata, entity-ref, and eval. If you are converting an XSL document into an XSLT document and these elements are used in the XSL document, you will have to write script that will replace these elements. This is what the Microsoft XSL to XSLT Converter did for the eval element. We'll discuss this converter in the next section.

The stylesheet element still exists in XSLT, but the attributes have been changed. The attributes for the stylesheet element are now: extension-element-prefixes, xmlns:xsl, and version. The extension-element-prefixes attribute is used to designate an extended namespace. The xmlns:xsl namespace must be http://www.w3.org/1999/XSL/Transform. You can include other namespaces.

The following table lists XSLT elements that are not part of XSL:

XSLT Elements

Name and Syntax Description

<xsl:include href = uri/>

The XSLT resource located by the href attribute value is parsed as an XML document, and the children of the xsl:stylesheet element in this document will replace the xsl:include element in the including document. This element is supported in the Internet Explorer 5 DOM.

<xsl:import href = uri/>

Imports another XSLT style sheet. Importing a style sheet is the same as including it except that definitions and template rules in the importing style sheet take precedence over template rules and definitions in the imported style sheet.

<xsl:call-template name = Name/>

A template element with a name attribute specifies a named template. An xsl:call-template element invokes a template by name; it has a required name attribute that identifies the template to be invoked. Unlike xsl:apply-templates, xsl:call-template does not change the current node or the current node list.

disable-output-escaping = "yes" | "no" />

Creates a text node with the string- value of the content of the text element in the result tree. Adjacent text nodes in the result tree are automatically merged. A template can also contain text nodes. Each text node in a template remains after white space has been stripped. The value of the disable-output-escaping attribute can be either yes or no.

<xsl:number level = "single" |
count = pattern
from = pattern
value = number-expression
format = { string }
lang = { nmtoken }
letter-value = { "alphabetic" | "traditional" }
grouping-separator = { char }
grouping-size = { number } />

Insert a formatted number into the result tree. The number to be inserted may be specified by an expression.

<xsl:sort select = "string"
lang = "nmtoken"
datatype = {"text"|"number"}
order = {"ascending"|"descending"}
case-order = {"upper-first"|"lower-first"} />

The XPath expression in select indicates the node on which the sort should be based. The sort element is supported in Internet Explorer 5, but only with the select and order attributes.

name = qualified-name
select = " XPath expression" />

Sets a variable for use within an XSLT style sheet. The name attribute is the name of the variable to be used within variable references. To reference the value of the variable, use the $variable-name syntax. This element is supported in the Internet Explorer 5 DOM.

name = qualified-name
select = "XPath expression" />

Declares a parameter for use within an XSLT style sheet. To reference the value of the parameter, use the $parameter-name syntax. This element is supported in Internet Explorer 5 DOM.

select = " XPath expression" />

Inserts a result tree fragment or node-set into the result tree. This element is supported by the Internet Explorer 5 DOM.

name = " qualified-name"
select = " XPath expression" />

Passes a parameter to a template. This element is supported by the Internet Explorer 5 DOM.

name = qualified-name
match = pattern
use = node-set-expression />

The key element is used to declare keys. This element provides information about the keys of the node that matches the pattern given in the match attribute.

name = qualified-name
decimal-separator = char
grouping-separator = char
infinity = string
minus-sign = char
NaN = string
percent = char
per-mille = char
zero-digit = char
digit = char
pattern-separator = char />

Declares a decimal-format, which controls the interpretation of a format pattern used by the format-number function. If there is a name attribute, the element declares a named decimal-format; otherwise, it declares the default decimal-format.

terminate ="yes"|"no"/>

Sends a message in a way that is dependent on the XSLT processor. The content of the message instruction is a template. You can instantiate the message by instantiating the content to create an XML fragment. This XML fragment is the content of the message. An XSLT processor might implement the message element by popping up an alert box or by writing to a log file.

If the terminate attribute has the value yes, then the XSLT processor should terminate processing after sending the message.

<xsl:fallback />

Normally, instantiating an xsl:fallback element does nothing. However, when an XSLT processor performs fallback for an instruction element, if the instruction element has one or more xsl:fallback children, then the content of each of the xsl:fallback children must be instantiated in sequence; otherwise, an error must be signaled.

method = "xml"|"html"|"text"
version = nmtoken
encoding = string
omit-xml-declaration = "yes"|"no"
standalone = "yes"|"no"
indent = "yes"|"no"
media-type = string />

Allows style sheet authors to specify how they want the result tree to be output. This element is allowed only as a top-level element, and it is supported by the Internet Explorer 5 DOM.