WebReference.com - Excerpt from Inside XSLT, Chapter 2, Part 3 (5/5)
[previous] |
Inside XSLT
Now I can add another template for the next level down, which includes the
<PLANET>
elements. In this case, I'll just replace each <PLANET>
element with the literal result element <P>Planet</P>
:
Listing 2.3: Using <xsl:apply-templates/>
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="https://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<HTML>
<xsl:apply-templates/>
</HTML>
</xsl:template>
<xsl:template match="PLANETS">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="PLANET">
<P>
Planet
</P>
</xsl:template>
</xsl:stylesheet>
Here's the result of this stylesheet:
<HTML>
<P>
Planet
</P>
<P>
Planet
</P>
<P>
Planet
</P>
</HTML>
As you can see, there is nothing left of the <PLANETS>
element at all.
All that's left is the three literal result elements <P>Planet</P>
that were
substituted for the three <PLANET>
elements.
Omitting the select Attribute
If you omit theselect
attribute, then only the child nodes of the current node are processed, which does not include attribute or namespace nodes, because they are not considered children. If you want to process those kinds of nodes, you'll have to use theselect
attribute, as you'll see in Chapter 3.
This is all very interesting, but not too useful. It would be far better, for example, to be able to access the actual value of each element (such as the name of each planet) and make use of that data. And, of course, you can. [Continued in part 4 - Ed.]
[previous] |
Created: September 26, 2001
Revised: September 26, 2001
URL: https://webreference.com/authoring/languages/xml/insidexslt/chap2/3/5.html