XSL最全基础教程之xsl:apply-templates元素
来源:网络收集 点击: 时间:2024-04-24语法
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/4创建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
fighting75/fighting
/hero
hero
name关羽/name
address河东解人/address
weapon青龙偃月刀/weapon
fighting100/fighting
/hero
hero
name张飞/name
address幽州涿郡/address
weapon丈八蛇矛/weapon
fighting95/fighting
/hero
/heros
分析:如果想使用Notepad++快速的编写xml文件,可以安装Zen Coding插件。

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

运行结果
用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
td75/td
td战斗力:75/td
/tr
tr
td style=font-size:14px;font-family:serif;关羽/td
td河东解人/td
td青龙偃月刀/td
td100/td
td战斗力:100/td
/tr
tr
td style=font-size:14px;font-family:serif;张飞/td
td幽州涿郡/td
td丈八蛇矛/td
td95/td
td战斗力:95/td
/tr
/tbody
/table
/body
/html

问题描述
使用Notepad++编写中文时,出现XML Parsing error:Input is not proper UTF-8,如下图:

解决方法
选择“Encoding”-Convert to UTF-8-BOM,保存即可

版权声明:
1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。
2、本站仅提供信息发布平台,不承担相关法律责任。
3、若侵犯您的版权或隐私,请联系本站管理员删除。
4、文章链接:http://www.1haoku.cn/art_581743.html