| |
Top Contents Index Glossary |
|
Link Summary
|
Glossary Terms |
The XML Stylesheet Language (XSL) has three major subcomponents:
The remainder of this section describes the XSLT package structure, and discusses the XPath addressing mechanism in a bit more depth.
There XSLT packages break down as follows:
The XPath specification is the foundation for a variety of specifications, including XSLT and linking/addressing specifications like XPointer. So an understanding of XPath is fundamental to a lot of advanced XML usage. This section provides a thorough introduction to XSLT, so you can refer to as needed later on.
Note:
In this tutorial, you won't actually use XPath until you get to the last page of this section, Transforming XML Data with XSLT. So, if you like, you can skip this section and go on ahead to the next page, Writing Out a DOM as an XML File. (When you get to the last page, there will be a note that refers you back here, so you don't forget!)
In general, an XPath expression specifies a pattern that selects a set of XML nodes. XSLT templates then use those patterns when applying transformations. (XPointer, on the other hand, adds mechanisms for defining a point or a range, so that XPath expressions can be used for addressing.)
The nodes in an XPath expression refer to more than just elements. They also refer to text and attributes, among other things. In fact, the XPath specification defines an abstract document model that defines seven different kinds of nodes:
Note:
The root element of the XML data is modeled by an element node. The XPath root node contains the document's root element, as well as other information relating to the document.
The data model is described in the last section of the XPath Specification, Section 5. (Like many specifications, it is frequently helpful to start reading near the end! Frequently, many of the important terms and underlying assumptions are documented there. That sequence has often been the "magic key" that unlocks the contents of a W3C specification.)
In this abstract model, syntactic distinctions disappear, and you are left with a normalized view of the data. In a text node, for example, it makes no difference whether the text was defined in a CDATA section, or if it included entity references;. The text node will consist of normalized data, as it exists after all parsing is complete. So the text will contain a "<" character, regardless of whether an entity reference like < or a CDATA section was used to include it. (Similarly for the "&" character.)
In this section of the tutorial, we'll deal mostly with element nodes and text nodes. For the other addressing mechanisms, see the XPath Specification.
An XML document is a tree-structured (hierarchical) collection of nodes. Like a hierarchical directory structure, it is useful to specify a path that points a particular node in the hierarchy. (Hence the name of the specification: XPath). In fact, much of the notation of directory paths is carried over intact:
In an xHTML document, for example, the path /h1/h2/ would indicate an h2 element under an h1. (Recall that in XML, element names are case sensitive, so this kind of specification works much better in xHTML than it would in HTML.)
In a pattern-matching specification like XSLT, the specification /h1/h2 selects all h2 elements that lie under an h1 element. To select a specific h2 element, square brackets ([]) are used for indexing (like those used for arrays). The path /h1[4]/h2[5] would therefore select the fifth h2 element under the fourth h1 element.
Note:
In xHTML, all element names are in lowercase. But as a matter of style, uppercase names are easier to read and easier to write about. (Although they are admittedly harder to write.) For the remainder of XPATH tutorial, then, and for the section on using XSLT transforms, all XML element names will be in uppercase. (Attribute names, on the other hand, will remain in lowercase.)
As you've seen, a name in XPath specification refers to an element. To refer to attribute, you prefix it's name with an "@" sign. For example, @type refers to the type attribute of an element. Assuming you have an XML document with list elements, for example, the expression list/@type selects the type attribute of the list element.
Note:
(Since the expression does not begin with /, the reference specifies a list node relative to the current context -- whatever position in the document that happens to be.)
The full range of XPath expressions takes advantage of the wildcards, operators, and functions that XPath defines. You'll be learning more about those shortly. Here, we'll take a look at a couple of the most common XPath expressions, simply to introduce the concept.
The expression @type="unordered" specifies an attribute named type whose value is "unordered". So an expression like LIST/@type specifies the type attribute of a LIST element.
But now for something a little different! In XPath, the square-bracket notation ([]) normally associated with indexing is extended to specify selection-criteria. For example, the expression LIST[@type="unordered"] selects all LIST elements whose type value is "unordered".
Similar expressions exist for elements, where each element has an associated string-value. (You'll see how the string-value is determined for a complicated element in a little while. For now, we'll stick with super-simple elements that have a single text string.)
Suppose you model what's going on in your organization with an XML structure that consists of PROJECT elements and ACTIVITY elements that have a text string with the project name, multiple PERSON elements to list the people involved and, optionally, a STATUS element that records the projects status. Here are some more examples that use the extended square-bracket notation:
The XPath specification defines quite a few addressing mechanisms, and they can be combined in many different ways. As a result, XPath delivers a lot of expressive power for a relatively simple specification. This section illustrates two more interesting combinations:
Note:
Many more combinations of address operators are listed in section 2.5 of the XPath Specification. This is arguably the most useful section of the spec for defining an XSLT transform.
By definition, an unqualified XPath expression selects a set of XML nodes that matches that specified pattern. For example, /HEAD matches all top-level HEAD entries, while /HEAD[1] matches only the first. But XPath expressions can also contain one of several wildcards to broaden the scope of the pattern matching:
* matches any element node (not attributes or text) node() matches all nodes of any kind: element nodes, text nodes, attribute nodes,
processing instruction nodes, namespace nodes, and comment nodes.@* matches all attribute nodes
In the project database example, for instance, /*/PERSON[.="Fred"] matches any PROJECT or ACTIVITY element that includes Fred.
So far, all of the patterns we've seen have specified an exact number of levels in the hierarchy. For example, /HEAD specifies any HEAD element at the first level in the hierarchy, while /*/* specifies any element at the second level in the hierarchy. To specify an indeterminate level in the hierarchy, use a double forward slash (//). For example, the XPath expression //PARA selects all paragraph elements in a document, wherever they may be found.
The // pattern can also be used within a path. So the expression /HEAD/LIST//PARA indicates all paragraph elements in a subtree that begins from /HEAD/LIST.
XPath expressions yield either a set of nodes,: a string, a boolean (true/false value), or a number. Expressions can also be created using one of several operations on these values:
| Alternative. So PARA|LIST selects all PARA and LIST elements. or, and Returns the or/and of two boolean values. =, != Equal or not equal, for booleans, strings, and numbers. <, >, <=, >= Less than, greater than, less than or equal to, greater than or equal to -- for numbers. +, -, *, div, mod Add, subtract, multiply, floating-point divide, and modulus (remainder) operations
(e.g. 6 mod 4 = 2)
Finally, expressions can be grouped in parentheses, so you don't have to worry about operator precedence. (Which, for those of you who who are good at such things, is roughly the same as that shown in the table.)
Before going on, it's worthwhile to understand how the string-value of more complex element is determined. We'll do that now.
The string-value of an element is the concatenation of all descendent text nodes, no matter how deep. So, for a "mixed-model" XML data element like this:
<PARA>This paragraph contains a <B>bold</B> word</PARA>
the string-value of <PARA> is "This paragraph contains a bold word". In particular, note that <B> is a child of <PARA> and that the text contained in all children is concatenated to form the string-value.
Also, it is worth understanding that the text in the abstract data model defined by XPath is fully normalized. So whether the XML structure contains the entity reference "<" or "<" in a CDATA section, the element's string-value will contain the "<" character. Therefore, when generating HTML or XML with an XSLT stylesheet, occurrences of "<" will have to be converted to < or enclosed in a CDATA section. Similarly, occurrence of "&" will need to be converted to &.
This section ends with an overview of the XPath functions. You can use XPath functions to select a collection of nodes in the same way that you would use an element-specification. Other functions return a string, a number, or a boolean value. For example, the expression /PROJECT/text( ) gets the string-value of project nodes.
Many functions depend on the current context. In the example above, the context for each invocation of the text() function is the PROJECT node that is currently selected.
There are many XPath functions -- too many to describe in detail here. This section provides a quick listing that shows the available XPath functions, along with a summary of what they do.
Note:
Skim the list of functions to get an idea of what's there. For more information, see Section 4 of the XPath Specification.
Many XPath expressions select a set of nodes. In essence, they return a node-set. One function does that, too.
These functions operate on or return strings.
Note:
XPath defines 3 ways to get the text of an element: text(), string(object), and the string-value implied by an element name in an expression like this: /PROJECT[PERSON="Fred"].
These functions operate on or return boolean values..
These functions convert one data type to another.
XPath operators, functions, wildcards, and node-addressing mechanisms can be
combined in wide variety of ways. The introduction you've had so far should
give you a good head start at specifying the pattern you need for any particular
purpose.
| |
Top Contents Index Glossary |