XSLT

一、XSLT简介

 XML样式表=XSL

    XSL转换=XSLT

    XSL(扩展样式表语言)在转换XML文档时分为明显的两个步骤:第一是转换文档结构;第二是将文档格式化输出。这两步可以分离开来并单独处理,因此XSL在发展过程中逐渐分裂为XSLT(结构转换)和XSL-FO(格式化输出)两种分支语言。

    将文件以XSL样式表来进行声明的文件头是<xsl:stylesheet>或<xsl:transform>      note:两者是完全同意的,两者都可以使用。

    根据W3C的XSLT标准,声明XSL样式表的正确代码如下:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">         或      <xsl:transformt version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

二、XSLT的基本转换过程

    在转换过程中,XSLT 使用 XPath 来定义源文档中可匹配一个或多个预定义模板的部分。一旦匹配被找到,XSLT 就会把源文档的匹配部分转换为结果文档。

    我们现在要把下面这个 XML 文档("cdcatalog.xml")转换为 XHTML:

    

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
.
.
.
</catalog>

创建 XSL 样式表

然后创建一个带有转换模板的 XSL 样式表("cdcatalog.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 align="left">Title</th>
      <th align="left">Artist</th>
    </tr>
    <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>
    </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

把 XSL 样式表链接到 XML 文档

向 XML 文档("cdcatalog.xml")添加 XSL 样式表引用:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
.
.
.
</catalog>

 

三、XSL元素简介

XSLT xsl:template 元素

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

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

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

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

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

<?xml version="1.0" encoding="ISO-8859-1"?>

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

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

<xsl:template> 元素内部的内容定义了写到输出结果的 HTML 代码。

最后两行定义了模板的结尾,及样式表的结尾。

 

XSLT  xsl:value-of  元素

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

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

 

<?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><xsl:value-of select="catalog/cd/title"/></td>
      <td><xsl:value-of select="catalog/cd/artist"/></td>
     </tr>
   </table>
 </body>
 </html>
</xsl:template>

</xsl:stylesheet>

注释:select 属性的值是一个 XPath 表达式。此表达式的工作方式类似于定位某个文件系统,在其中正斜杠可选择子目录。

XSLT  xsl:for-each  元素

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

<xsl:for-each> 元素可用于选取指定的节点集中的每个 XML 元素。

<?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>
      <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>
    </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

注释:select 属性的值是一个 XPath 表达式。此表达式的工作方式类似于定位某个文件系统,在其中正斜杠可选择子目录。

结果过滤:

通过在 <xsl:for-each> 元素中添加一个选择属性的判别式,我们也可以过滤从 XML 文件输出的结果。

<xsl:for-each select="catalog/cd[artist='Bob Dylan']">

合法的过滤运算符:

  • =  (等于)
  • != (不等于)
  • &lt; (小于)
  • &gt; (大于)
<?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>
   <xsl:for-each select="catalog/cd[artist='Bob Dylan']">
   <tr>
      <td><xsl:value-of select="title"/></td>
      <td><xsl:value-of select="artist"/></td>
   </tr>
   </xsl:for-each>
  </table>
 </body>
 </html>
</xsl:template>

</xsl:stylesheet>

XSLT  xsl:sort 元素

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

如需对结果进行排序,只要简单地在 XSL 文件中的 <xsl:for-each> 元素内部添加一个 <xsl:sort> 元素:

<?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>
      <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>
    </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

注释:select 属性指示需要排序的 XML 元素。

XSLT  xsl:if 元素

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

如需放置针对 XML 文件内容的条件测试,请向 XSL 文档添加 <xsl:if> 元素。

语法:

<xsl:if test="expression">
  ...
  ...如果条件成立则输出...
  ...
</xsl:if>

在何处放置 <xsl:if> 元素:

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

<?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>
      <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>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

注释:必选的 test 属性的值包含了需要求值的表达式。

上面的代码仅仅会输出价格高于 10 的 CD 的 title 和 artist 元素。

XSLT  xsl:choose  元素

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

语法:

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

在何处放置选择条件:

要插入针对 XML 文件的多重条件测试,请向 XSL 文件添加 <xsl:choose>、<xsl:when> 以及 <xsl:otherwise>:

<?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>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title"/></td>
          <xsl:choose>
          <xsl:when test="price &gt; 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>
    </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

上面的代码会在 CD 的价格高于 10 时向 "Artist" 列添加粉色的背景颜色。

另一个例子:

这是另外一个包含两个 <xsl:when> 元素的例子:

<?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>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title"/></td>
          <xsl:choose>
          <xsl:when test="price &gt; 10">
            <td bgcolor="#ff00ff">
            <xsl:value-of select="artist"/></td>
          </xsl:when>
          <xsl:when test="price &gt; 9">
            <td bgcolor="#cccccc">
            <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>
    </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

上面的代码会在 CD 的价格高于 10 时向 "Artist" 列添加粉色的背景颜色,并在 CD 的价格高于 9 且低于等于 10 时向 "Artist" 列添加灰色的背景颜色。

XSLT  xsl:apply-templates  元素

<xsl:apply-templates> 元素可把一个模板应用于当前的元素或者当前元素的子节点。

<xsl:apply-templates> 元素可把一个模板应用于当前的元素或者当前元素的子节点。

假如我们向 <xsl:apply-templates> 元素添加一个 select 属性,此元素就会仅仅处理与属性值匹配的子元素。我们可以使用 select 属性来规定子节点被处理的顺序。

请看下面的 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> 
<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>

 XSLT  xsl:variable 元素

定义和用法:

<xsl:variable> 元素用于声明局部或全局的变量。

注释:如果被声明为顶层元素,则该变量是全局的,而如果在模板内声明,则变量是本地的。

注释:一旦您设置了变量的值,就无法改变或修改该值!

提示:您可以通过 <xsl:variable> 元素的内容或通过 select 属性,向变量添加值!

语法 :

<xsl:variable
name="name"
select="expression">

  <!-- Content:template -->

</xsl:variable>

属性:

name name 必需。规定变量的名称。
select expression 可选。定义变量的值。

实例:

例子 1

如果设置了 select 属性,<xsl:variable> 元素就不能包含任何内容。如果 select 属性含有文字字符串,则必须给字符串加引号。

下面的两个例子为变量 "color" 赋值 "red":

<xsl:variable name="color" select="'red'" />
<xsl:variable name="color" select='"red"' />

例子 2

如果 <xsl:variable> 元素只包包含 name 属性,且没有内容,则变量的值是空字符串:

<xsl:variable name="j" />

例子 3

下面的例子通过 <xsl:variable> 元素的内容为变量 "header" 赋值:

<?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="header">
  <tr>
  <th>Element</th>
  <th>Description</th>
  </tr>
</xsl:variable>

<xsl:template match="/">
  <html>
  <body>
  <table>
    <xsl:copy-of select="$header" />
    <xsl:for-each select="reference/record">
    <tr>
    <xsl:if category="XML">
      <td><xsl:value-of select="element"/></td>
      <td><xsl:value-of select="description"/></td>
    </xsl:if>
    </tr>
    </xsl:for-each>
  </table>
  <br />
  <table>
    <xsl:copy-of select="$header" />
    <xsl:for-each select="table/record">
    <tr>
    <xsl:if category="XSL">
      <td><xsl:value-of select="element"/></td>
      <td><xsl:value-of select="description"/></td>
    </xsl:if>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

 XSLT  xsl:param  元素

定义和用法

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

注释:如果在模板内声明参数,就是局部参数,如果作为顶层元素来声明,就是全局参数。

语法:

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

<!-- Content:template -->

</xsl:param>

属性:

属性描述
name name 必需。规定参数的名称。
select expression 可选。规定 XPath 表达式,该表达式是参数的默认值。

实例:

例子 1

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

 

posted @ 2018-01-15 22:31  kudangren  阅读(485)  评论(0编辑  收藏  举报