XSLT学习二——元素下

       上一篇写了XSLT的根元素和顶级元素。

       3、指令元素

        xsl:apply-imports元素可应用来自导入xslt中的模版规则。导入(import)XSLT中的模板规则的优先级要比主XSLT中的模板规则要低。如果要使用导入XSLT中的某条摸版规则,而不是主XSLT中的某个等价规则就会用到xsl:apply-imports元素。

       xsl:apply-templates元素可向当前元素或当前元素的子元素应用模板。如果我们向 xsl:apply-templates 元素添加 select 属性,那么它仅会处理匹配该属性的值的子元素。我们可使用 select 属性来规定处理子介点的顺序。

       示例xml:

<?xml version="1.0" encoding="utf-8" ?>
<
data>
<
book>
<
title>Book1</title>
</
book>
<
book>
<
title>Book2</title>
</
book>
<
cd>
<
title>CD1</title>
</
cd>
</
data>

        例子1:用 h1 元素包围文档中每个 title 元素:

<?xml version="1.0" encoding="utf-8"?>
<
xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<
xsl:template match="title">
<
h1>
<
xsl:apply-templates/>
</
h1>
</
xsl:template>
</
xsl:stylesheet>

        例子2:用 h1 元素包围文档中所有属于 book的子元素的 title 元素:

<?xml version="1.0" encoding="utf-8"?>
<
xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<
xsl:template match="book">
<
h1>
<
xsl:apply-templates select="title"/>
</
h1>
</
xsl:template>
</
xsl:stylesheet>

      xsl:attribute元素用于向元素添加属性。xsl;attribute元素还用于xsl:attribute-set元素集合中。

<?xml version="1.0" encoding="utf-8"?>
<
xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<
xsl:template match="/">
<
input type="text">
<
xsl:attribute name="value">This is a test attribute.</xsl:attribute>
</
input>
</
xsl:template>
</
xsl:stylesheet>

 

       xsl:call-template元素调用指定模板。调用的模板可以是本XSLT的,也可以使导入(import)的XSLT。

<?xml version="1.0" encoding="utf-8"?>
<
xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<
xsl:template match="/">
<
xsl:call-template name="title"/>
</
xsl:template>

<
xsl:template name="title">
</
xsl:template>
</
xsl:stylesheet>

 

       xsl:choose元素与xsl:when以及xsl:otherwise元素结合,可表达多重条件测试。如果所有的xsl:when都不为true,则处理xsl:otherwise内容。 

<?xml version="1.0" encoding="utf-8"?>
<
xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<
xsl:template match="/">
<
xsl:variable name="name">
<
xsl:choose>
<
xsl:when test="message/type=1">
Name1
</xsl:when>
<
xsl:when test="message/type=2">
Name2
</xsl:when>
<
xsl:otherwise>
None
</xsl:otherwise>
</
xsl:choose>
</
xsl:variable>
</
xsl:template>
</
xsl:stylesheet>

 

      xsl:comment元素用于在结果树中创建注释节点。

<?xml version="1.0" encoding="utf-8"?>
<
xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<
xsl:template match="/">
<
xsl:comment>This is comment content</xsl:comment>
</
xsl:template>
</
xsl:stylesheet>

结果为:

<?xml version="1.0" encoding="utf-16"?><!--This is comment content-->

 

      xsl:copy元素可创建当前节点的一个副本(拷贝)。

xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<
xsl:template match="/">
<
xsl:copy>
<
xsl:apply-templates/>
</
xsl:copy>
</
xsl:template>
</
xsl:stylesheet>

       xsl:copy-of元素可创建当前节点的一个副本。当前节点的 Namespace 节点、子节点以及属性都会被自动复制!

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

<?xml version="1.0" encoding="utf-8"?>
<
xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<
xsl:template match="/">
<
xsl:element name="message">
<
xsl:element name="title">Test Title</xsl:element>
</
xsl:element>
</
xsl:template>
</
xsl:stylesheet>

结果为:

<?xml version="1.0" encoding="utf-16"?><message><title>Test Title</title></message>

 

       xsl:fallback元素规定了在 XSL 处理程序不支持 XSL 元素时,所执行的替代代码。

       xsl:for-each元素可遍历指定的节点集中的每个节点。

<?xml version="1.0" encoding="utf-8"?>
<
xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<
xsl:template match="/">
<
xsl:for-each select="data/book/title">
<
xsl:value-of select="."/>
<
xsl:if test="position()!=last()">,</xsl:if>
</
xsl:for-each>
</
xsl:template>
</
xsl:stylesheet>

结果为:Book1,Book2

      

      xsl:if元素用于当teste为true执行规则。

      xsl:message元素可向输出写一条消息。该元素主要用于报告错误。该元素能够包含几乎任何其他的 XSL 元素(<xsl:text> 、<xsl:value-of> 等等)。terminate 属性允许您选择在错误发生时,是否应终止转换。

<?xml version="1.0" encoding="utf-8"?>
<
xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<
xsl:template match="/">
<
xsl:message terminate="yes">
Error: Alert test message!
</xsl:message>
</
xsl:template>
</
xsl:stylesheet>

       xsl:number元素用于测定在源中当前节点的整数位置。它也用于将格式化的数字插入结果树。

       xsl:processing-instruction元素可向输出写一条处理指令,即生成处理指令节点。

<?xml version="1.0" encoding="utf-8"?>
<
xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<
xsl:template match="/">
<
xsl:processing-instruction name="xml-stylesheet">
href="style.css" type="text/css"
</xsl:processing-instruction>
</
xsl:template>
</
xsl:stylesheet>

结果为:<?xml-stylesheet href="style.css" type="text/css"?>

 

      xsl:sort元素用于对结果进行排序。

<?xml version="1.0" encoding="utf-8"?>
<
xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<
xsl:template match="/">
<
xsl:for-each select="data/book">
<
xsl:sort select="orderno"/>
<
xsl:value-of select="title"/>
<
xsl:if test="position()!=last()">,</xsl:if>
</
xsl:for-each>
</
xsl:template>
</
xsl:stylesheet>

xml:

<?xml version="1.0" encoding="utf-8" ?>
<
data>
<
book>
<
orderno>2</orderno>
<
title>Book1</title>
</
book>
<
book>
<
orderno>1</orderno>
<
title>Book2</title>
</
book>
</
data>

结果为:Book2,Book1

 

      xsl:text元素用于向输出写文本,即通过样式表生成文本节点。该元素可包含文本、实体引用,以及 #PCDATA。disable-output-escaping属性默认值为 "no"。如果值为 "yes",通过实例化xsl:text元素生成的文本节点在输出时将不进行任何转义。比如如果设置为 "yes",则 "<" 将不进行转换。如果设置为 "no",则被输出为 "&lt;"。

<?xml version="1.0" encoding="utf-8"?>
<
xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<
xsl:template match="/">
<
xsl:text>This is a test text</xsl:text>
</
xsl:template>
</
xsl:stylesheet>

     xsl:value-of元素可用于选取某个 XML 元素的值,并把它输出。select 属性(必选)的值是一个 XPath 表达式。disable-output-escaping默认值为 "no"。如果值为 "yes",通过实例化xsl:text元素生成的文本节点在输出时将不进行任何转义。比如如果设置为 "yes",则 "<" 将不进行转换。如果设置为 "no",则被输出为 "&lt;"。

<?xml version="1.0" encoding="utf-8"?>
<
xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<
xsl:template match="/">
<
table width="100">
<
xsl:for-each select="data/book">
<
tr>
<
td>
<
xsl:value-of select="title"/>
</
td>
</
tr>
</
xsl:for-each>
</
table>
</
xsl:template>
</
xsl:stylesheet>
结果为:<table width="100"><tr><td>Book1</td></tr><tr><td>Book2</td></tr></table>。
posted @ 2009-11-27 01:20  Asharp  阅读(914)  评论(0编辑  收藏  举报