<xsl:apply-templates/>的应用
一.xml和xslt代码举例
View Code
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type='text/xsl' href='abc.xslt'?>
<collection>
<cd>
<title>Boys for Pele</title>
<artist>Tori Amos</artist>
<tracks>
<track type="vocal">
<name>Horses</name>
<length>3.5</length>
</track>
<track type="instrumental">
<name>Blood roses</name>
<length>3.2</length>
</track>
<track type="vocal">
<name>Father lucifer</name>
<length>3.8</length>
</track>
<track type="instrumental">
<name>Professional widow</name>
<length>4.1</length>
</track>
<track type="vocal">
<name>Mr. Zebra</name>
<length>3.6</length>
</track>
</tracks>
</cd>
<cd>
<title>The Ghosts that Hunt me</title>
<artist>Crash TestDummies</artist>
<tracks>
<track type="vocal">
<name>Winter song</name>
<length>4.3</length>
</track>
<track type="instrumental">
<name>Comin's back soon</name>
<length>4.1</length>
</track>
<track type="vocal">
<name>Superman's song</name>
<length>4.1</length>
</track>
<track type="vocal">
<name>Here on earth</name>
<length>3.1</length>
</track>
</tracks>
</cd>
</collection
<?xml-stylesheet type='text/xsl' href='abc.xslt'?>
<collection>
<cd>
<title>Boys for Pele</title>
<artist>Tori Amos</artist>
<tracks>
<track type="vocal">
<name>Horses</name>
<length>3.5</length>
</track>
<track type="instrumental">
<name>Blood roses</name>
<length>3.2</length>
</track>
<track type="vocal">
<name>Father lucifer</name>
<length>3.8</length>
</track>
<track type="instrumental">
<name>Professional widow</name>
<length>4.1</length>
</track>
<track type="vocal">
<name>Mr. Zebra</name>
<length>3.6</length>
</track>
</tracks>
</cd>
<cd>
<title>The Ghosts that Hunt me</title>
<artist>Crash TestDummies</artist>
<tracks>
<track type="vocal">
<name>Winter song</name>
<length>4.3</length>
</track>
<track type="instrumental">
<name>Comin's back soon</name>
<length>4.1</length>
</track>
<track type="vocal">
<name>Superman's song</name>
<length>4.1</length>
</track>
<track type="vocal">
<name>Here on earth</name>
<length>3.1</length>
</track>
</tracks>
</cd>
</collection
View Code
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body></body>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="collection">
<xsl:apply-templates select="cd[title='Boys for Pele']"/>
</xsl:template>
<xsl:template match="cd">
<center>
<font size="5">
<b>
<xsl:value-of select="title"/>:
</b>
</font>
<font size="5">
<i>
<xsl:value-of select="artist"/>
</i>
</font>
</center>
<xsl:apply-templates select="tracks"/>
</xsl:template>
<xsl:template match="tracks">
<center>
<table border="1">
<tr>
<th>Name</th>
<th>Length</th>
<th>Type</th>
</tr>
<xsl:for-each select="track">
<tr>
<td>
<xsl:value-of select="name"/>
</td>
<td>
<xsl:value-of select="length"/>
</td>
<td>
<xsl:value-of select="@type"/>
</td>
</tr>
</xsl:for-each>
</table>
<br/>
<br/>
</center>
</xsl:template>
</xsl:stylesheet
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body></body>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="collection">
<xsl:apply-templates select="cd[title='Boys for Pele']"/>
</xsl:template>
<xsl:template match="cd">
<center>
<font size="5">
<b>
<xsl:value-of select="title"/>:
</b>
</font>
<font size="5">
<i>
<xsl:value-of select="artist"/>
</i>
</font>
</center>
<xsl:apply-templates select="tracks"/>
</xsl:template>
<xsl:template match="tracks">
<center>
<table border="1">
<tr>
<th>Name</th>
<th>Length</th>
<th>Type</th>
</tr>
<xsl:for-each select="track">
<tr>
<td>
<xsl:value-of select="name"/>
</td>
<td>
<xsl:value-of select="length"/>
</td>
<td>
<xsl:value-of select="@type"/>
</td>
</tr>
</xsl:for-each>
</table>
<br/>
<br/>
</center>
</xsl:template>
</xsl:stylesheet
二. 说明
<xsl:template match="cd">
<xsl:apply-templates/>
</xsl:template>
匹配所有的cd的子节点
<xsl:apply-templates/>的作用是通知解析器把当前匹配的节点的所有子节点和已定义的模板进行匹配,如有相符的则运用对应的模板,如无模板匹配的节点将按文本形式显示对应的内容;默认 select="."即时cd下的所有子节点,这里包括title artist tracks节点,因为title和artist节点没有找到匹配的模版,所以输出文本.
通常会选择指定的节点,例如 <xsl:apply-templates select="tracks"/>,即选择的集合是cd下的所有tracks节点