码迷,mamicode.com
首页 > 其他好文 > 详细

XSLT主要元素

时间:2014-07-11 20:46:32      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   使用   文件   os   

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

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

2、<xsl:template> 元素

<xsl:template> 元素用于构建模板。

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

如果存在为相同元素定义的多个处理方法,那么用 mode 可以区分它们。

<xsl:template match="/">

<table border="1">

<xsl:apply-templates select="employees/employee" mode="table"/>

</table>

<xsl:apply-templates select="employees/employee" mode="list"/>

</xsl:template>

<xsl:template match="employee" mode="table">

<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:template>

<xsl:template match="employee" mode="list">

<ul>

<li><xsl:value-of select="name"/></li>

<li><xsl:value-of select="age"/></li>

<li><xsl:value-of select="monthly_pay"/></li>

</ul>

</xsl:template>

注意:根据实际测试发现,如果某个节点元素没有定义其template,或者定义其template为空<xsl:template match="元素名称" />,则其下的子孙节点将不会被处理(即使为其子孙节点定义了template)。

3、<xsl:value-of> 元素用于提取某个选定节点的值。

<xsl:value-of> 元素用于提取某个选定节点的值,并把值添加到转换的输出流中:

<td><xsl:value-of select="catalog/cd/title"/></td>

<td><xsl:value-of select="catalog/cd/artist"/></td>

4、<xsl:for-each> 元素允许您在 XSLT 中进行循环。

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

5、<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>

6、<xsl:if> 元素

<xsl:if> 元素用于放置针对 XML 文件内容的条件测试。

语法:

<xsl:if test="expression">

...

...如果条件成立则输出...

...

</xsl:if>

如需添加有条件的测试,请在 XSL 文件中的 <xsl:for-each> 元素内部添加 <xsl:if> 元素:

<xsl:for-each select="catalog/cd">

<xsl:if test="price &gt; 10">

<tr>

<td><xsl:value-of select="title"/></td>

<td><xsl:value-of select="artist"/> </td>

</tr>

</xsl:if>

</xsl:for-each>

7、<xsl:choose> 元素

XSLT <xsl:choose> 元素用于组合 <xsl:when> 和 <xsl:otherwise> 来表达多重条件测试。

语法:

<xsl:choose>
<xsl:when test="expression">
... 输出 ...
</xsl:when>
<xsl:otherwise>
... 输出 ....
</xsl:otherwise>
</xsl:choose>

8、<xsl:apply-templates> 元素

<xsl:apply-templates> 元素可把一个模板应用于当前的元素或者当前元素的所有子节点,向 <xsl:apply-templates> 元素添加一个 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>

注意:如果当前节点的直接子节点没有匹配的模板,则默认会输出该元素的文本。

如果当前节点的直接子节点没有匹配的模板,则处理器会选择其孙子节点的模板进行处理。

9、XSLT <xsl:attribute> 元素

用于向输出元素(非xsl:element创建的元素)添加属性。

注释:<xsl:attribute> 元素会替换名称相同的已有属性。

<xsl:attribute name="attributename" namespace="uri">
<!-- Content:template -->
</xsl:attribute>

name属性为必须的,而namespace为可选的。

<xsl:attribute-set name="font">

<xsl:attribute name="fname">Arial</xsl:attribute>

<xsl:attribute name="size">14px</xsl:attribute>

<xsl:attribute name="color">red</xsl:attribute>

</xsl:attribute-set>

例子:

<test>

<xsl:attribute name="addAttribute" >TestAttribute</xsl:attribute>

</test>

10、<xsl:attribute-set> 元素可创建命名的属性集。该属性集(attribute-set)可作为整体应用到输出文档。

<xsl:attribute-set

name="name" use-attribute-sets="name-list">

<!-- Content:xsl:attribute* -->

</xsl:attribute-set>

name属性为必须,而use-attribute-sets为可选的,定义引用其他的属性集定义,多个使用空格分隔。

11、<xsl:call-template> 元素可调用一个指定的模板。

<xsl:call-template name="templatename">

<!-- Content:xsl:with-param* -->

</xsl:call-template>

在希望调用的时候直接调用即可,无需节点或者属性的匹配,也就是说,它会忽略所调用的模板(模板名称为templatename)的匹配模式(match)。

与apply-template元素不一样,那个是对当前元素的所有子元素进行调用指定的模板(根据匹配条件)。

或者说:apply-template是从元素匹配的角度进行模板的调用,而call-template则是根据模板名称进行模板调用。

Comment:它不同于apply-template是对当前的每个符合select的元素都循环调用对应的模板,而call-template更像是函数调用,因为它可以传递参数,对于需要所调用模板处理的元素可以使用<xsl:with-param >进行传递。

12、<xsl:with-param> 元素定义了传递给模板的参数的值。

注释:<xsl:with-param> 元素的 name 属性的值必须与 <xsl:param> 元素中的 name 相匹配,否则将忽略 <xsl:with-param> 元素,。

<xsl:with-param name="name" select="expression">

<!-- Content:template -->

</xsl:with-param>

13、<xsl:param> 元素用于声明局部或全局参数。

<xsl:param

name="name"

select="expression">

<!-- Content:template -->

</xsl:param>

其中,name是必须的,而select为xpath,是参数的默认值。

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="xx">
<html>
<body>
<xsl:call-template name="show_title">
<xsl:with-param name="title" />
</xsl:call-template>
</body>
</html>
</xsl:variable>
<xsl:template name="show_title" match="/">
<xsl:param name="title" />
<xsl:for-each select="catalog/cd">
<p>Title: <xsl:value-of select="$title" /></p>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

无论是<xsl:value-of或者<xsl:for-each>都使用 $变量名 引用参数值。

注意:<xsl:param> 元素上指定的值是绑定的默认值。如果调用的模板或样式表包含 <xsl:param>,传递的参数将替代默认值。

<xsl:param> 元素必须声明为 <xsl:template> 元素的直接子级。如果未声明为直接子级,<xsl:param> 元素的值将无法访问,并将出错。

14、<xsl:element> 元素

<xsl:element> 元素用于在输出文档中创建元素节点。

<xsl:element
name="name"
namespace="URI"
use-attribute-sets="namelist">
<!-- Content:template -->
</xsl:element>

name为必须,namespace、use-attribute-sets为可选,同时name及namespace可为表单式,由处理器在运行时运算得到结果。

注意:由XSL:element所创建的元素,为其添加属性必须使用use-attribute-sets来指定,而不能直接使用xsl:attribute来为其添加属性。

15、<xsl:message> 元素

定义和用法

<xsl:message> 元素可向输出写一条消息。该元素主要用于报告错误。

该元素能够包含几乎任何其他的 XSL 元素(<xsl:text> 、<xsl:value-of> 等等)。

terminate 属性允许您选择在错误发生时,是否应终止转换。

<xsl:message terminate="yes|no">

<!-- Content:template -->

</xsl:message>

输出的消息:不会放在输出文档中,而是会在控制台中红色显示所输出的消息。

XSLT主要元素,布布扣,bubuko.com

XSLT主要元素

标签:style   http   color   使用   文件   os   

原文地址:http://www.cnblogs.com/ghpaas/p/3833342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!