6.3. Fine-Tuning Templates

With the basics of template rules and selection down, let's look at some ways to customize the process. This section covers node creation, generating numbers and text, and sorting and looping through node sets.

6.3.1. Outputting Node Values with <xsl:value-of>

The default rule for elements and attributes is to output the contents as text. So far, we have no other way of outputting the values of nodes. <xsl:value-of> is the tool for calculating and returning values. To see how it works on nodes, consider the following rule:

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

If we turn this rule loose on the <quotelist> example, we get this result:

publication
The New Scientist
6/99

tv-show
Homer
The Simpsons

Greg Travis

Confucius

The values for each type of node are calculated as follows:

Root

The root node inherits the value of the document element.

Element

All the parsed character data in the element, together with the values of its descendants.

Attribute

The value of the attribute with entities resolved, and leading and trailing whitespace removed.

Text

All the text in the node.

Processing instruction

Everything inside the processing instruction delimiters except the name.

Comment

The text inside the comment delimiters.

Namespace

The namespace's URI.

If <xsl:value-of> is applied to a node set, only the first node's value is used. We might be tempted to use the following rule, but it would return only the value of the first ...

Get Learning XML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.