- XSLT 教程
- XSLT - 主頁
- XSLT - 概述
- XSLT - 語法
- XSLT - <template>
- XSLT - <value-of>
- XSLT - <for-each>
- XSLT - <sort>
- XSLT - <if>
- XSLT - <choose>
- XSLT - <key>
- XSLT - <message>
- XSLT - <apply-template>
- XSLT - <import>
- 有用的 XSLT 資源
- XSLT - 快速指南
- 有用的 XSLT 資源
- XSLT - 討論
XSLT <apply-template>
<xsl:apply-template> 標記觸發 XSLT 處理器根據每個選定節點的型別和上下文查詢要應用的恰當模板。
宣告
以下是 <xsl:apply-template> 元素的語法宣告。
<xsl:apply-template select = Expression mode = QName > </xsl:apply-template>
屬性
| 序號 | 名稱和描述 |
|---|---|
| 1 | select 用於處理由 XPath 表示式選定的節點,而不是處理所有子節點。 |
| 2 | mode 允許指定其限定名稱的元素多次處理,每次產生不同的結果。 |
元素
| 出現次數 | 無限 |
|---|---|
父元素 |
xsl:attribute、xsl:comment、xsl:copy、xsl:element、xsl:fallback、xsl:foreach、xsl:if、xsl:message、xsl:otherwise、xsl:param、xsl:processinginstruction、xsl:template、xsl:variable、xsl:when、xsl:with-param、output 元素 |
子元素 |
xsl:sort、xsl:with-param |
演示示例
此示例透過遍歷每個學生來建立包含 <student> 元素的列表,該元素及其屬性 rollno 和其子元素 <firstname>、<lastname>、<nickname> 和 <marks>。
students.xml
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "students.xsl"?>
<class>
<student rollno = "393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno = "493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno = "593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
</class>
students.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>Students</h2>
<xsl:apply-templates select = "class/student" />
</body>
</html>
</xsl:template>
<xsl:template match = "class/student">
<xsl:apply-templates select = "@rollno" />
<xsl:apply-templates select = "firstname" />
<xsl:apply-templates select = "lastname" />
<xsl:apply-templates select = "nickname" />
<xsl:apply-templates select = "marks" />
<br />
</xsl:template>
<xsl:template match = "@rollno">
<span style = "font-size = 22px;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "firstname">
First Name:<span style = "color:blue;">
<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:red;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
<xsl:template match = "marks">
Marks:<span style = "color:gray;">
<xsl:value-of select = "." />
</span>
<br />
</xsl:template>
</xsl:stylesheet>
輸出
廣告