O'Reilly logo

Learning XML by Erik T. Ray

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required