xsl:apply-templates 模板的应用

<?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>

<head>

<title>测试xsl:apply-templates元素</title>

</head>

<body>

<table border="1" cellspacing="0" cellpadding="0">

<tr>

<th>姓名</th>

<th>出身地</th>

<th>武器</th>

<th>战斗力</th>

<th>战斗力数值</th>

</tr>

<xsl:apply-templates select="heros/hero"/>

</table>

</body>

</html>

</xsl:template>

<xsl:template match="hero">

<tr>

<xsl:apply-templates select="name"/>

<xsl:apply-templates select="address"/>

<xsl:apply-templates select="weapon"/>

<xsl:apply-templates select="fighting"/>

<xsl:apply-templates select="fighting" mode="detail"/>

</tr>

</xsl:template>

<xsl:template match="name">

<td style="font-size:14px;font-family:serif;">

<xsl:apply-templates/>

</td>

</xsl:template>

<xsl:template match="address">

<td>

<xsl:apply-templates/>

</td>

</xsl:template>

<xsl:template match="weapon">

<td>

<xsl:apply-templates/>

</td>

</xsl:template>

<xsl:template match="fighting">

<td>

<xsl:apply-templates />

</td>

</xsl:template>

<xsl:template match="fighting" mode="detail">

<td>

战斗力:<xsl:apply-templates />

</td>

</xsl:template>

</xsl:stylesheet>

注意:

如果<xsl:apply-templates>元素没有添加select属性,xsl处理器会处理当前节点所有子集。

而语句<xsl:apply-templates select="heros/hero"/>中,添加select属性(其属性值是XPath表达式),xsl处理器会处理匹配XPath表达式的子节点并在上下文找到适合应用的模板,同时,可以使用select属性规定xsl处理器处理子节点的顺序。

原文地址:https://www.cnblogs.com/threestars/p/10468818.html