This section provides a tutorial example on how to use 'value-of' elements to take string values of parts of the source XML document. The string value of an element is only the text content of the element and its child elements.
As we learned from the previous section, the "value-of" element can be used to evaluate any XPath expressions.
But "value-of" elements are most commonly used to with XPath node set expressions to take the string values
of specified parts of the source XML file. For example:
<!-- take the string value of the current element -->
<xsl:value-of select="."/>
<!-- take the string value of the specified attribute -->
<xsl:value-of select="@attribute_name"/>
<!-- take the string value of the specified child element -->
<xsl:value-of select="child_element_name"/>
The string value of an element is actually the same as the output of the default template.
It contains only the text content of the element and its child elements. Attribute values are not included.
The string value of an attribute is the text string associated with the attribute.
As a tutorial example, let's add a XSL link to my dictionary.xml, and save it as dictionary_xsl.xml:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="dictionary.xsl"?>
<dictionary>
<!-- dictionary_xsl.xml
Copyright (c) 2002 by Dr. Herong Yang. http://www.herongyang.com/
-->
<word acronym="true">
<name>XML</name>
<definition referenece="Herong's Notes">eXtensible Markup
Language.</definition>
<update date="2002-12-23"/>
</word>
<word symbol="true">
<name><</name>
<definition>Mathematical symbol representing the "less than" logical
operation, like: 1<2.</definition>
<definition>Reserved symbol in XML to representing the beginning of
tags, like: <![CDATA[<p>Hello world!</p>]]>
</definition>
</word>
</dictionary>
Then write a simple XSL file, dictionary.xsl, to transform it into
a tree display with all the information in the source XML file:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- dictionary.xsl, version 1.0
Copyright (c) 2002 by Dr. Herong Yang. http://www.herongyang.com/
-->
<xsl:template match="dictionary">
<pre>
dictionary
<xsl:apply-templates/>
</pre>
</xsl:template>
<xsl:template match="word">
word
|-acronym=<xsl:value-of select="@acronym"/>
|-symbol=<xsl:value-of select="@symbol"/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="name">
name
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="definition">
definition
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="update">
date
|-acronym=<xsl:value-of select="@date"/>
</xsl:template>
</xsl:stylesheet>
Openning dictionary_xsl.xml with Internet Explorer, I got:
dictionary
word
|-acronym=true
|-symbol=
name
XML
definition
eXtensible Markup
Language.
date
|-acronym=2002-12-23
word
|-acronym=
|-symbol=true
name
<
definition
Mathematical symbol representing the "less than" logical
operation, like: 1<2.
definition
Reserved symbol in XML representing the beginning of
tags, like: <p>Hello world!</p>
Note that:
The line breaks in text contents of source element were maintained by the "value-of"
statement.
The entities, like <, in text content of source element were also maintained by
the "value-of" statement. They were presented by IE with their values, like <.
The comment in the source file was ignored. But the link break that starts the comment
line was maintained. This is why I got a blank line under "dictionary".
The "value-of" statements on undefined attributes returned empty strings.