1、<xsl:value-of>元素
<xsl:value-of>元素是選擇目前節點的值,用于在結果樹中建立一個文本節點。例如<xsl:value-of select="Name" />就是選擇Name節點的值。
select屬性是必須的,用于指定要計算的表達式,表達式計算的結果将被轉換為一個字元串值。如果字元串為空,那麼文本節點将不會被建立。
2、<xsl:for-each>元素
<xsl:for-each>元素逐個(select)選擇某條件,應用條件。select屬性是必須的,用于指定一個表達式,該表達式計算結果必須是一個節點集。<xsl:for-each>元素的内容是一個模闆,對于每一個被選擇的節點,執行個體化該模闆。
for-each還支援排序,order-by語句,它的文法是以分号(;)分隔、作為排序标準的清單。在清單元素前添加加号(+)表示按此标記的内容以升序排序,添加減号(-)表示逆序排序。作為一種簡化的表示就是,排序标準清單就是由select規定的标記的子标記的序列,每個标記之間以(;)分隔。
employee.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="src/employees44.xsl"?>
<employees>
<employee sn="E-200402100001">
<name>zhangsan</name>
<age>25</age>
<monthly_pay mode="cash">
1200.00
</monthly_pay>
</employee>
<employee sn="E-200402100006">
<name>lisi</name>
<age>28</age>
1600.00
<employee sn="E-200503220001">
<name>wangwu</name>
<age>30</age>
<monthly_pay mode="credit_card">
3500.00
</employees>
employee.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head></head>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="employees">
<body>
<table border="1">
<xsl:for-each select="employee">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="age"/></td>
<td><xsl:value-of select="monthly_pay"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</xsl:stylesheet>
通過xslt處理器轉換為html結果為:
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<table border="1">
<tr>
<td>zhangsan</td>
<td>25</td>
<td>1200.00</td>
</tr>
<td>lisi</td>
<td>28</td>
<td>1600.00</td>
<td>wangwu</td>
<td>30</td>
<td>3500.00</td>
</table>
</body>
</html>
本文轉自 leizhimin 51CTO部落格,原文連結:http://blog.51cto.com/lavasoft/60416,如需轉載請自行聯系原作者