假设您有以下源 XML:
<?xml version="1.0" encoding="UTF-8"?>
<list>
<student>
<course>two</course>
<course>three</course>
</student>
<student>
<course>ten</course>
<course>nine</course>
<course>six</course>
</student>
</list>
并应用此 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<list>
<xsl:apply-templates/>
</list>
</xsl:template>
<xsl:template match="student">
<xsl:param name="pos">
<xsl:value-of select="position()"/>
</xsl:param>
<number>
<xsl:text>Student number </xsl:text>
<xsl:value-of select="$pos"/>
<xsl:text> has </xsl:text>
<xsl:value-of select="count(course)"/>
<xsl:text> courses.</xsl:text>
</number>
</xsl:template>
</xsl:样式表>
你得到这个输出:
<?xml version="1.0" encoding="UTF-8"?>
<list>
<number>Student number 1 has 2 courses.</number>
<number>Student number 2 has 3 courses.</number>
</list>