XSLT <xsl:template>-Element

XSL 样式表由一个或多套被称为模板(template)的规则组成。

每个模板含有当某个指定的节点被匹配时所应用的规则。

元素

元素用于构建模板。

match 属性用于关联 XML 元素和模板。match 属性也可用来为整个文档定义模板。match 属性的值是 XPath 表达式(举例,match="/" 定义整个文档)。

好了,让我们看一下上一节中的 XSL 文件的简化版本:




 
 
   

My CD Collection

Title Artist
. .

代码解释:

由于 XSL 样式表本身也是一个 XML 文档,因此它总是由 XML 声明起始:


下一个元素,,定义此文档是一个 XSLT 样式表文档(连同版本号和 XSLT 命名空间属性)。

元素定义了一个模板。而 match="/" 属性则把此模板与 XML 源文档的根相联系。

元素内部的内容定义了写到输出结果的 HTML 代码。

The last two lines define the end of the template and the end of the stylesheet.

The result of the above transformation is similar to this:

View XML File,View XSL File,View Result

The result of this example has a slight defect because the data has not been copied from the XML document to the output.

In the next section, you will learn how to use <xsl:value-of> Elements select values from XML elements.