XML

Pattern Essentials

Patterns have crept into the XSLT discussion several times throughout this tutorial and the previous tutorial. I'd like to mention one more time that you explore patterns more thoroughly in Tutorial 22. However, XSLT uses patterns enough that I think it would be helpful to cheat a little and give you a quick primer on how to use them. This section isn't intended to make you a pattern expert, but it will hopefully give you some insight into how patterns fit into XSLT.

As you know by now, patterns are used to address parts of XML documents much as paths in file systems are used to address folders and files. Patterns can be used to isolate specific nodes or groups of nodes and can be specified as absolute or relative. An absolute pattern spells out the exact location of a node or node set, whereas a relative pattern identifies a node or node set relative to a certain context. In the previous contacts.xsl example, the pattern contacts/contact is an absolute pattern, whereas the pattern name is a relative pattern. The name pattern is relative because it makes an assumption about the current context.

Patterns are used in several situations throughout XSLT, but the majority of the time you'll use them to set the select and match attributes of standard XSLT elements. The simplest pattern is the pattern that references the current node, which is a simple period (.). Following is an example of how to use this pattern:

<xsl:value-of select="."/>

The current node pattern is obviously highly dependent upon the context of the document. A pattern that isn't dependent upon context is the root pattern, which is identified with a single forward slash (/). The root pattern identifies the location of a document's root element no matter where it appears. To create an absolute pattern, you must begin with the root element and specify the exact hierarchy of nodes leading to a node or node set.

Other patterns are used to reference nodes that are above or below the current node. For example, a child node pattern is created by simply specifying the name of the node. A parent node, on the other hand, is created using two periods (..). Following is an example of using a pattern to access a parent node:

<xsl:value-of select=".."/>

You can put patterns together to get more interesting results. For example, to address a sibling node, you must first go to the parent and then reference the sibling as a child. In other words, you use the parent pattern (..) followed by a forward slash (/) followed by the sibling node name, as in the following example:

<xsl:value-of select="../brother"/>

If you want to select all of the child nodes of a given node, you can use the double slash (//) pattern, as in the following example:

<xsl:value-of select="//"/>

Lest you think patterns are limited to elements, you can easily address attributes by specifying the attribute name preceded by an at symbol (@), as in the following example:

<xsl:value-of select="info/@ssnum"/>

This code assumes that the current node contains a child element named info that has an attribute named ssnum.

by BrainBellupdated
Advertisement: