当前位置: 首页 > 网络学院 > XML相关教程 > XSL/XSLT > <xsl:template>

XSL/XSLT
XSL 语言
XSLT 介绍
XSLT 浏览器
XSLT 转换
<xsl:template>
<xsl:value-of>
<xsl:for-each>
<xsl:sort>
<xsl:if>
<xsl:choose>
<xsl:apply-templates>
XSLT - 客户端
XSLT - 服务器端
XSLT - 编辑XML
XSLT 摘要
XSLT 元素参考
XSLT 函数
XSL 编辑器

XSL/XSLT 中的 <xsl:template>


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 557 ::
收藏到网摘: n/a

An XSL style sheet consists of one or more set of rules that are called templates.
XSL样式表是由一个或多个被称为“模板” 的规则集组成的。

Each template contains rules to apply when a specified node is matched.
每个模板都包含了与每一个指定节点相匹配的应用规则。


The <xsl:template> Element
XSLT<xsl:template>元素

The <xsl:template> element is used to build templates.
<xsl:template>元素是用于创建模板的。

The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document).
Match 属性的作用是使模板和XML与元素相匹配。Match属性也可以为整个XML文档定义模版。Match属性值是一个XPath表达式。(例如:match="/" 定义整个文档)。

Ok, let's look at a simplified version of the XSL file from the previous chapter:
接下来,让我们看一下上一章中XSL文件的简化版本:

<?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>

<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>

</tr>
<tr>
<td>.</td>
<td>.</td>
</tr>

</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Since an XSL style sheet is an XML document itself, it always begins with the XML declaration: <?xml version="1.0" encoding="ISO-8859-1"?>.
因为XSL样式表就是XML文档本身,所以它一般都从XML开始声明:<?xml version="1.0" encoding="ISO-8 59-1"?>

The next element, <xsl:stylesheet>, defines that this document is an XSLT style sheet document (along with the version number and XSLT namespace attributes).
下一个元素<xsl:stylesheet>,把这份文档定义为XSLT样式表文档(连同版本号和XSLT命名空间属性一起定义)。

The <xsl:template> element defines a template. The match="/" attribute associates the template with the root of the XML source document.
<xsl:template>元素定义了一份模板。Match=”/”属性使模板和XML源文档的根目录相互联结起来。

The content inside the <xsl:template> element defines some HTML to write to the output.
<xsl:template>元素里面的内容定义一些HTML来对结果进行书写。

The last two lines define the end of the template and the end of the style sheet.
最后两行定义了模版的结束符和样式表的结束符。

The result of the transformation above will look like this:
通过上面的代码,转换的结果如下:

My CD Collection
我的CD集

Title Artist
. .

View the XML fileView the XSL file, and View the result
查看XML文件, 查看 XSL文件, 以及 最终结果

The result from this example was a little disappointing, because no data was copied from the XML document to the output.
这个例子所产生的结果有点令人失望,因为没有将任何数据从XML文档中复制到结果当中。

In the next chapter you will learn how to use the <xsl:value-of> element to select values from the XML elements.
在下一章当中,你将会学习如何使用<xsl:value-of>元素从XML元素中选择指定的值。

评论 (0) All

登陆 | 还没注册?