由于 XSL 样式表本身也是一个 XML 文档,因此它总是由 XML 声明起始:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
//匹配根元素
<xsl:template match="/">
<body>
//遍历匹配元素,转化为HTML
</body>
</html>
</xsl:template>
1.<xsl:templete></xsl:templete>
<xsl:template>
元素——<xsl:template>
元素用于构建模板。
match
属性用于关联 XML 元素
和模板
。match
属性也可用来为整个文档定义模板。match
属性的值是 XPath
表达式(举例,match="/" 定义整个文档
)
2.<xsl:value-of> 元素
<xsl:value-of> 元素用于提取某个选定节点的值,并把值添加到转换的输出流中
<tr>
<td><xsl:value-of select="catalog/cd/title"/></td>
<td><xsl:value-of select="catalog/cd/artist"/></td>
</tr>
3. <xsl:for-each> 元素
<xsl:for-each> 元素可用于选取指定的节点集中的每个 XML 元素。
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
注释:select 属性的值是一个 XPath 表达式。此表达式的工作方式类似于定位某个文件系统,在其中正斜杠可选择子目录。
结果过滤
通过在 <xsl:for-each> 元素中添加一个选择属性的判别式,我们也可以过滤从 XML 文件输出的结果。
合法的过滤运算符:
= (等于)
!= (不等于)
< (小于)
> (大于)
<xsl:for-each select="catalog/cd[artist='Bob Dylan']">
4.<xsl:sort> 元素
<xsl:sort> 元素用于对结果进行排序。
如需对结果进行排序,只要简单地在 XSL 文件中的 <xsl:for-each> 元素内部添加一个 <xsl:sort>
元素:
<xsl:for-each select="catalog/cd">
<xsl:sort select="artist"/>
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
5.<xsl:if> 元素
<xsl:if> 元素用于放置针对 XML 文件内容的条件测试。
如需放置针对 XML 文件内容的条件测试,请向 XSL 文档添加<xsl:if>
元素。
语法
<xsl:if test="expression">
...
...如果条件成立则输出...
...
</xsl:if>
<xsl:for-each select="catalog/cd">
<xsl:if test="price > 10">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:if>
</xsl:for-each>
6. <xsl:choose> 元素
<xsl:choose> 元素用于结合 <xsl:when> 和 <xsl:otherwise> 来表达多重条件测试
语法
<xsl:choose>
<xsl:when test="expression">
... 输出 ...
</xsl:when>
<xsl:otherwise>
... 输出 ....
</xsl:otherwise>
</xsl:choose>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<xsl:choose>
<xsl:when test="price > 10">
<td bgcolor="#ff00ff">
<xsl:value-of select="artist"/></td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="artist"/></td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
7. <xsl:apply-templates> 元素
<xsl:apply-templates>
元素可把一个模板应用于当前的元素或者当前元素的子节点**
<xsl:apply-templates> 元素
<xsl:apply-templates> 元素可把一个模板应用于当前的元素或者当前元素的子节点。
假如我们向 <xsl:apply-templates> 元素添加一个 select 属性,此元素就会仅仅处理与属性值匹配的子元素。我们可以使用 select 属性来规定子节点被处理的顺序
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="cd">
<p>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="artist"/>
</p>
</xsl:template>
<xsl:template match="title">
Title: <span style="color:#ff0000">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
<xsl:template match="artist">
Artist: <span style="color:#00ff00">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
</xsl:stylesheet>