Examples of Using "value-of" Elements

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) 2014, HerongYang.com, All Rights Reserved.
-->
 <word acronym="true">
  <name>XML</name>
  <definition reference="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) 2014, HerongYang.com, All Rights Reserved.
-->
 <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>

Opening 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:

Last update: 2014.

Table of Contents

 About This Book

 Introduction of XML (eXtensible Markup Language)

 XML File Syntax

 XML File Browsers

 DOM (Document Object Model) Programming Interface

 SAX (Simple API for XML) Programming Interface

 DTD (Document Type Definition) Introduction

 Syntaxes of DTD Statements

 Validating an XML Document against the Specified DTD Document Type

 XSD (XML Schema Definition) Introduction

 Syntaxes of XSD Statements

 Validating XML Documents Against Specified XML Schemas

 XSL (Extensible Stylesheet Language) Introduction

 XSLT (XSL Transformations) Introduction

 Java Implementation of XSLT

 XPath (XML Path) Language

XSLT Elements as Programming Statements

 "value-of" - Evaluating XPath String Expressions

Examples of Using "value-of" Elements

 "{expression}" - Shorthand of "value-of" Elements

 "variable" - Declaring Variables

 "for-each" - Looping through a Node Set

 Examples of Using "for-each" Elements

 "if" - The Conditional Element

 "choose" - The If...Else Element

 Control and Generate XML Element in the Result

 XML Notepad - XML Editor

 XML Tools Plugin for Notepad++

 XML 1.1 Changes and Parsing Examples

 Outdated Tutorials

 References

 PDF Printing Version