xsl:for-each
xsl:copy
xsl:copy-of
xsl:template
xsl:text
ELEMENT: xsl:value-of
<xsl:value-of
select="expression"
disable-output-escaping="yes" | "no"
/>
The
xsl:value-of
element is used to write or display in the result tree a string representation of the value assigned to a specified node. To explain it in another way, this XSLT element causes the value assigned to an XML tag to be displayed as text in the HTML page that we create to display the information in our XML file. There is no limit to the number of
xsl:value-of
elements that can appear in the code. However, each
xsl:value-of
element can only have one node assigned to it and this is accomplished by using the mandatory
select
attribute. Remember that a node can occur an unlimited number of times in an XML document. (For example, the
<phone> ... </phone>
xml tags in our DevGuru Staff List can be used for each staff member.)
You can use the
xsl:for-each
element to access each occurrence of a specified node in the order in which they occur in the document. As each occurrence is accessed, you can use the
xsl:value-of
element to display the value of the current node. As an example, you can use the
xsl:for-each
element to access all of the phone numbers in the
<phone> ... </phone>
XML tags in our DevGuru Staff List and then display the numbers using the
xsl:value-of
element. Additionally, you can use the
xsl:sort
element to define a sorting key which can be used to sort values.
You can also use the
xsl:copy, xsl:copy-of,
and
xsl:text
elements to display values as a string or text.
If the value is not a string, then the value will be converted to a string. (This is done automatically by calling the
String
function.) If the value is a Boolean, then the string will display as "true" or "false". If the value is a node-set, then only the first value of the first node in the set is displayed and the values of all other nodes in the set are completely ignored.
This is a self-closing element and it cannot contain any child elements or any content.
select="expression"
The mandatory
select
attribute assigns the node (by name) to the element. Only one node can be assigned for each occurrence of this element.
disable-output-escaping="yes" | "no"
The optional
disable-output-escaping
attribute specifies how special characters should appear in the output string. A yes dictates that special characters should be displayed as is (i.e., a < or >). The default, no, dictates that special characters should NOT be displayed as is (i.e., a < is displayed as <).
We use the
DevGuru Staff List XML file
for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_valueof.xsl"?>
and we name it: xslt_example_valueof.xml
In this example, we use the
xsl:for-each
and
xsl:value-of
elements to display all of the values assigned to the name, dob, age, address, and phone tags (nodes):
Code for xslt_example_valueof.xsl:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template>
<html>
<body>
<xsl:for-each select="devguru_staff/programmer">
<div>
NAME: <
xsl:value-of select="name" /
>
<br />
DOB: <
xsl:value-of select="dob" /
>
<br />
AGE: <
xsl:value-of select="age" /
>
<br />
ADDRESS: <
xsl:value-of select="address" /
>
<br />
PHONE: <
xsl:value-of select="phone" /
>
<hr />
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
Click to view output in separate window
- requires Internet Explorer
Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information