<xsl:apply-templates>
是你的朋友:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="html" />
<xsl:template match="root">
<h1>List of books</h1>
<xsl:apply-templates />
</xsl:template>
<xsl:template match="book">
<xsl:apply-templates select="title" />
<xsl:apply-templates select="description" />
</xsl:template>
<xsl:template match="title">
<h2>
<a name="{.}"/>
<xsl:value-of select="." />
</h2>
</xsl:template>
<xsl:template match="description">
<p>
<xsl:apply-templates />
</p>
</xsl:template>
<xsl:template match="description//*">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="description//link">
<a href="#{@ref}">
<xsl:apply-templates />
</a>
</xsl:template>
<xsl:template match="text()" />
<xsl:template match="description//text()">
<xsl:copy-of select="." />
</xsl:template>
</xsl:stylesheet>
为输入XML生成以下输出(
注意:为了可读性,我把它整齐地排好。过程中删除了不相关的空白
):
<h1>List of books</h1>
<h2><a name="Stuff">Stuff</h2>
<p>This book is <i>great</i> if you need to know about stuff. I
suggest <a href="#Things">this one</a> if you need to know about
things.</p>