|
XSL - Getting Values Out Of Source Elements
Part:
1
2
3
After learned how and when to transform an element and its child elements in the XML source
file, now we are going to look at how to retrieve values out of the different parts of the
source element.
This tutorial describes:
- The "value-of" Element
- The "for-each" Element
- The "if" Element
- The "choose" Element
The 'value-of' Element
value-of: An XSL element, serving as an evaluation statement. It computes the string value
of a specified part of an element in source XML file. The result of "value-of" statement is inserted
into the output.
<xsl:value-of select="."/>
<xsl:value-of select="@attribute_name"/>
<xsl:value-of select="child_element_name"/>
where "." specifies to compute the string value of the current element, "@attribute_name"
specifies to compute the string value of a given attribute, and "child_element_name"
specifies to compute the string value of a given child element.
The string value of an element is actually the same as the output of the default template.
It contains only the text parts of the content of the element and its child elements.
The string value of an attribute is the text string associated with the attribute.
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
-->
<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
-->
<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>
(Continued on next part...)
Part:
1
2
3
|