XSL最全基础教程之xsl:apply-templates元素

2025-06-30 23:07:29

1、语法<xsl:apply-templates select=Expression mode=QName></xsl:apply-templates>属性select(可选) 该属性值是XPath表达式,可以用于处理表达式选择的节点,而不是处理所有子节点。如果省略属性,可以选取当前节点的子节点。mode(可选) mode属性值允许xsl解析器可以多次处理匹配节点,每次可以产生不同的结果。如果<xsl:template>没有match属性,就不可能有mode属性。 如果<xsl:apply-templates>元素有mode属性,该元素只适用于带有相同mode属性值的<xsl:template>模板规则。

2、创建xml文件创建名为heros.xml文件<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="heros.xsl"?><heros> <hero> <name>刘备</name> <address>涿郡涿县人</address> <weapon>双股剑</weapon> <fighting>75</fighting> </hero> <hero> <name>关羽</name> <address>河东解人</address> <weapon>青龙偃月刀</weapon> <fighting>100</fighting> </hero> <hero> <name>张飞</name> <address>幽州涿郡</address> <weapon>丈八蛇矛</weapon> <fighting>95</fighting> </hero></heros>分析:如果想使用Notepad++快速的编写xml文件,可以安装Zen Coding插件。

XSL最全基础教程之xsl:apply-templates元素

3、创建XSL文件创建一个名为heros.xsl文件<?xml version="1.0" encodin爿讥旌护g="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="name"/>处理name节点的所有子节点并在上下文找到适合应用的模板。 <xsl:apply-templates select="fighting" mode="detail"/>当select属性值相同,会根据mode值找到对应的模板<xsl:template match="fighting" mode="detail">。

XSL最全基础教程之xsl:apply-templates元素

4、运行结果用Firefox打开本地文件heros.xml粘贴html代码如下:<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>测试xsl:apply-templates元素</title> </head> <body> <table cellspacing="0" cellpadding="0" border="1"> <tbody> <tr> <th>姓名</th> <th>出身地</th> <th>武器</th> <th>战斗力</th> <th>战斗力数值</th> </tr> <tr> <td style="font-size:14px;font-family:serif;">刘备</td> <td>涿郡涿县人</td> <td>双股剑</td> <td>75</td> <td>战斗力:75</td> </tr> <tr> <td style="font-size:14px;font-family:serif;">关羽</td> <td>河东解人</td> <td>青龙偃月刀</td> <td>100</td> <td>战斗力:100</td> </tr> <tr> <td style="font-size:14px;font-family:serif;">张飞</td> <td>幽州涿郡</td> <td>丈八蛇矛</td> <td>95</td> <td>战斗力:95</td> </tr> </tbody> </table> </body> </html>

XSL最全基础教程之xsl:apply-templates元素
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢