XSLT <xsl: apply-template>
XSLT <xsl: apply-template>元素
XSLT <xsl: apply-template>元素用于告诉XSLT处理器根据要使用的模板找到合适的模板。每个选定节点的类型和上下文。
<xsl:apply-template
select = Expression
mode = QName>
</xsl:apply-template>
参数说明
名称 |
说明 |
选择 |
它用于处理XPath表达式从所有节点及其子节点列表中选择的节点。 |
模式 |
它用于允许对由其合格名称指定的元素进行多次处理,每次产生不同的结果。 |
XSLT <xsl: apply-template>元素示例
让我们以创建一个具有属性" id"及其子元素的<employee>元素列表为例。通过遍历每位员工来<firstname>,<lastname>,<nickname>和<salary>。
Employee.xml
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "employee.xsl"?>
<class>
<employee id = "001">
<firstname>Aryan</firstname>
<lastname>Gupta</lastname>
<nickname>Raju</nickname>
<salary>30000</salary>
</employee>
<employee id = "024">
<firstname>Sara</firstname>
<lastname>Khan</lastname>
<nickname>Zoya</nickname>
<salary>25000</salary>
</employee>
<employee id = "056">
<firstname>Peter</firstname>
<lastname>Symon</lastname>
<nickname>John</nickname>
<salary>10000</salary>
</employee>
</class>
Employee.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>
<body>
<h2>Employees</h2>
<xsl:apply-templates select = "class/employee" />
</body>
</html>
</xsl:template>
<xsl:template match = "class/employee">
<xsl:apply-templates select = "@id" />
<xsl:apply-templates select = "firstname" />
<xsl:apply-templates select = "lastname" />
<xsl:apply-templates select = "nickname" />
<xsl:apply-templates select = "salary" />
<br />
</xsl:template>
<xsl:template match = "@id">
<span style = "font-size = 25px;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "firstname">
First Name:<span style = "color:brown;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "lastname">
Last Name:<span style = "color:green;">
<xsl:value-of select = "." />
</span>
<br />
<</xsl:template>
<xsl:template match = "nickname">
Nick Name:<span style = "color:blue;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "salary">
Marks:<span style = "color:red;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
</xsl:stylesheet>
输出:
