XSL

指扩展样式表语言(EXtensible Stylesheet Language)。

XSL - 不仅仅是样式表语言,包括三部分:

  • XSLT :一种用于转换 XML 文档的语言。
  • XPath :一种用于在 XML 文档中导航的语言。
  • XSL-FO :一种用于格式化 XML 文档的语言。

XSLT

指 XSL 转换。在此教程中,你将学习如何使用 XSLT 将 XML 文档转换为其他文档,比如 XHTML。

XSLT 使用 XPath 在 XML 文档中进行导航。

样式表声明

把文档声明为 XSL 样式表的根元素是 <xsl:stylesheet> 或 <xsl:transform>。

注释: <xsl:stylesheet> 和 <xsl:transform> 是完全同义的,均可被使用!

根据 W3C 的 XSLT 标准,声明 XSL 样式表的正确方法是:

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

或者:

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

如需访问 XSLT 的元素、属性以及特性,我们必须在文档顶端声明 XSLT 命名空间。

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

如果您使用的浏览器兼容 XSLT,它会很顺利地把您的 XML 转换为 XHTML。

<xsl:template> 元素

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

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

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

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

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

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

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

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

以上转换的结果类似这样:


元素<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 表达式。此表达式的工作方式类似于定位某个文件系统,在其中正斜杠可选择子目录。

上面的转换结果类似这样:

这个例子的结果有一点缺陷:仅有一行数据从 XML 文档被拷贝到输出结果。

在下面的章节中,你将学习如何使用 <xsl:for-each> 元素来循环遍历 XML 元素,并显示所有的记录。

<xsl:for-each> 元素

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

结果过滤

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

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

合法的过滤运算符:

  • =  (等于)

  • != (不等于)
  • &lt; (小于)
  • &gt; (大于)

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

<xsl:if> 元素

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

如需添加有条件的测试,请在 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>

<xsl:choose> 元素

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

要插入针对 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>

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

<xsl:variable> 元素

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

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

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

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

下面的例子通过 <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>

最新文章

  1. Java动态代理全面分析
  2. LeetCode 201 Bitwise AND of Numbers Range 位运算 难度:0
  3. 帝国cms教程:帝国cms在列表页使用sys_ResizeImg函数自动生成不同大小的缩略图
  4. php中curl、fsockopen的应用
  5. awk 处理
  6. (转帖)oracle sql 语句优化
  7. Windows Server 2016-图形化备份域控制器
  8. sort命令与cat区别25.1 由于sort默认是把结果输出到标准输出,所以需要用重定向才能将结果写入文件,形如sort filename &gt; newfile [root@shiyan a]# cat a.txt aaaaaa [root@shiyan a]# sort a.txt &gt;c.txt ------------- 在重定向前会自动创建c.txt这个文件。 [root@shiyan
  9. nmon - 性能监控利器介绍
  10. 关于Newtonsoft.Json,LINQ to JSON的一个小demo
  11. Spring 源码学习(4)—— bean的加载part 1
  12. Zuul 跨域
  13. Response.Redirect &amp; window.location.href
  14. python logging日志输出个文件中
  15. 基于Cookie的购物车
  16. 外部容器出现塌陷现象(伪类after、before的使用)
  17. Nodejs 发送邮件
  18. 前端程序员必知的30个Chrome扩展-[转载]
  19. Log4j配置(转)
  20. C#导入PFX和Cer证书的工具类

热门文章

  1. Jira 自定义工作流并设置触发器
  2. 《ucore lab1》实验报告
  3. Mac上Docker的安装
  4. 021 Android 查询已经导入到工程中的数据库+抖动效果
  5. 【转帖】linux date 显示指定时区的时间 借助TZ 环境变量 export TZ=Asia/Shanghai 或 America/New_York
  6. linux服务器安装oracle
  7. C++开源库大全
  8. vue的交互
  9. 【转】Java基础——面试题汇总
  10. 常见的CSS样式