- Transforming XML with XSLT
- Beginning an XSLT Style Sheet
- Creating the Root Template
- Outputting HTML Code
- Outputting a Node's Content
Outputting a Node's Content
Once you've created the HTML code that will format a given node's content, you'll want to actually output that content (called its string value). The simplest way to add the content of a node (like an element) to the output document is to write it out just as it is.
To output a node's content:
-
If desired, create the HTML code that will format the content.
-
Type <xsl:value-of.
-
Type select="expression", where expression identifies the node set from the XML source document whose content should be output at this point.
-
Type />.
An excerpt of the XML source document shows the name element and its contents.
Tips
-
Use select="." to output the content of the current node. For more information on writing expressions, consult Chapter 11, XPath: Patterns and Expressions.
-
If the node set identified by the expres-sion has more than one node, only the first node's string value is output. (In the example in Figure 11, a node set with two nodes are found since there are two nodes that satisfy the expression. How-ever, because of this rule, only the value of the first ("Tiger") is output.
Instead of the generic "Endangered animals", we're going to grab the contents of the name element (that is within an animal element within an endangered_species element, and has a language attribute with the value set to English).
-
The string value of a node is generally the text that that node contains. If the node has child elements, however, the string value includes the text contained in those child elements as well.
-
If the node set is empty, there is nothing to output.
-
Some versions of Explorer's XSLT processor don't require the select attribute (and assume the current node if it is missing). It is, however, officially required.
-
Since the <xsl:value-of> element never has any content, you can always combine the opening and closing tags as shown in step 4 above.
-
If the expression evaluates to a boolean, the output may be either "true" or "false". If the expression is a number, the number is converted to a string.
-
You can also create an expression that calculates a value using functions.
When the XSLT processor applies the root template, it first outputs all the HTML header stuff, and then when it gets to the xsl:value-of element, it finds a nodeset with two nodes: the content of the animal name elements whose language is set to English. It then outputs the value of just the first of these nodes, which is Tiger.
And now our output actually uses input from the source XML document. There's definite poten-tial here.