<h1>surgery log</h1>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="section">
<p>
<b>
<xsl:value-of select="@time" />
</b>
</p>
<xsl:apply-templates />
</xsl:template>
<xsl:template match="tool">
<i>
<xsl:apply-templates />
</i>
</xsl:template>
<xsl:template match="procedure">
<font color="#ff8800">
<xsl:apply-templates />
</font>
</xsl:template>
</xsl:stylesheet>
请注意:因为模板是以正确的次序自动处理的,所以嵌套项是怎么样的不是现在主要关心的问题。您可以处理更复杂的情形,如清单 8 中的情形,其中,procedure 元素包含了一个 tool 元素:
清单 8. 带嵌套元素的样本叙述
<surgerylog>
<narrative>
<section time="13:00:00">first, the surgeon <procedure>used the
<tool>3-inch scalpel</tool> to make a two-inch incision</procedure>
in the patient's <location>lower left abdomen</location>.
<procedure>cautery</procedure> was then applied to the
<target>severed blood vessels</target> to stop the
bleeding.</section>
<section time="13:14:23">the surgeon then <procedure>cleaned and
dressed the incision</procedure>, using <tool>four two-by-two
sterile gauze pads</tool> and <tool>medium sutures</tool>.</section>
</narrative>
</surgerylog>
当将样式表应用于该源文档时,结果正如期望的那样:嵌在过程中的工具是斜体并且是橙色的。现在,请看一下用拉样式创建的相同样式表:
清单 9. 叙述的样本拉样式表
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform"
version="1.0">
<xsl:template match="surgerylog/narrative">
<html>
<body>
<h1>surgery log</h1>
<xsl:for-each select="section">
<p>
<b>
<xsl:value-of select="./@time" />
</b>
</p>
<p>
<xsl:for-each select="*|text()">
<xsl:choose>
<xsl:when test="name()='procedure'">
<font color="#ff8800">
<xsl:value-of select="." />
</font>
</xsl:when>
<xsl:when test="name()='tool'">
<i>
<xsl:value-of select="." />
</i>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="." />