zoukankan      html  css  js  c++  java
  • 信步漫谈之XSLT—基础介绍

    主要介绍XSLT(XSL Transformations),基于对W3C教程学习后的一些总结。

    XSL 指扩展样式表语言(EXtensibleStylesheetLanguage)。
    万维网联盟开始发展 XSL 的起因是由于对基于 XML 的样式表语言的需求。
    XSLT 指 XSL 转换。可以使用 XSLT 将 XML 文档转换为其他文档,比如 XHTML 。

    一、XSL包括三部分

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

        XSLT 用于将一种 XML 文档转换为另外一种 XML 文档,或者可被浏览器识别的其他类型的文档,比如 HTML 和 XHTML 。通常, XSLT 是通过把每个 XML 元素转换为 (X)HTML 元素来完成这项工作的。 通过 XSLT,我们可以向或者从输出文件添加或移除元素和属性。也可重新排列元素,执行测试并决定隐藏或显示哪个元素,等等。
        描述转化过程的一种通常的说法是, XSLT 把 XML 源树转换为XML结果树

        XSLT 使用 XPath 在 XML 文档中查找信息。XPath 被用来通过元素和属性在 XML 文档中进行导航。

    二、XSLT的工作原理

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

        正确的样式表声明
        把文档声明为 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 命名空间。
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 指向了官方的 W3C XSLT 命名空间。如果使用此命名空间,就必须包含属性 version="1.0"。

    三、实例展示

    XML文档:

    <?xml version="1.0" encoding="utf-8"?>
    <catalog> 
      <cd> 
        <title>Empire Burlesque</title>  
        <artist>Bob Dylan</artist>  
        <country>USA</country>  
        <company>Columbia</company>  
        <price>10.90</price>  
        <year>1985</year> 
      </cd>  
      <cd> 
        <title>Hide your heart</title>  
        <artist>Bonnie Tyler</artist>  
        <country>UK</country>  
        <company>CBS Records</company>  
        <price>9.90</price>  
        <year>1988</year> 
      </cd>  
      <cd> 
        <title>Greatest Hits</title>  
        <artist>Dolly Parton</artist>  
        <country>USA</country>  
        <company>RCA</company>  
        <price>9.90</price>  
        <year>1982</year> 
      </cd>  
      <cd> 
        <title>Still got the blues</title>  
        <artist>Gary Moore</artist>  
        <country>UK</country>  
        <company>Virgin records</company>  
        <price>10.20</price>  
        <year>1990</year> 
      </cd>  
      <cd> 
        <title>Eros</title>  
        <artist>Eros Ramazzotti</artist>  
        <country>EU</country>  
        <company>BMG</company>  
        <price>9.90</price>  
        <year>1997</year> 
      </cd>  
      <cd> 
        <title>One night only</title>  
        <artist>Bee Gees</artist>  
        <country>UK</country>  
        <company>Polydor</company>  
        <price>10.90</price>  
        <year>1998</year> 
      </cd>  
      <cd> 
        <title>Sylvias Mother</title>  
        <artist>Dr.Hook</artist>  
        <country>UK</country>  
        <company>CBS</company>  
        <price>8.10</price>  
        <year>1973</year> 
      </cd>  
      <cd> 
        <title>Maggie May</title>  
        <artist>Rod Stewart</artist>  
        <country>UK</country>  
        <company>Pickwick</company>  
        <price>8.50</price>  
        <year>1990</year> 
      </cd>  
      <cd> 
        <title>Romanza</title>  
        <artist>Andrea Bocelli</artist>  
        <country>EU</country>  
        <company>Polydor</company>  
        <price>10.80</price>  
        <year>1996</year> 
      </cd>  
      <cd> 
        <title>When a man loves a woman</title>  
        <artist>Percy Sledge</artist>  
        <country>USA</country>  
        <company>Atlantic</company>  
        <price>8.70</price>  
        <year>1987</year> 
      </cd>  
      <cd> 
        <title>Black angel</title>  
        <artist>Savage Rose</artist>  
        <country>EU</country>  
        <company>Mega</company>  
        <price>10.90</price>  
        <year>1995</year> 
      </cd>  
      <cd> 
        <title>1999 Grammy Nominees</title>  
        <artist>Many</artist>  
        <country>USA</country>  
        <company>Grammy</company>  
        <price>10.20</price>  
        <year>1999</year> 
      </cd>  
      <cd> 
        <title>For the good times</title>  
        <artist>Kenny Rogers</artist>  
        <country>UK</country>  
        <company>Mucik Master</company>  
        <price>8.70</price>  
        <year>1995</year> 
      </cd>  
      <cd> 
        <title>Big Willie style</title>  
        <artist>Will Smith</artist>  
        <country>USA</country>  
        <company>Columbia</company>  
        <price>9.90</price>  
        <year>1997</year> 
      </cd>  
      <cd> 
        <title>Tupelo Honey</title>  
        <artist>Van Morrison</artist>  
        <country>UK</country>  
        <company>Polydor</company>  
        <price>8.20</price>  
        <year>1971</year> 
      </cd>  
      <cd> 
        <title>Soulsville</title>  
        <artist>Jorn Hoel</artist>  
        <country>Norway</country>  
        <company>WEA</company>  
        <price>7.90</price>  
        <year>1996</year> 
      </cd>  
      <cd> 
        <title>The very best of</title>  
        <artist>Cat Stevens</artist>  
        <country>UK</country>  
        <company>Island</company>  
        <price>8.90</price>  
        <year>1990</year> 
      </cd>  
      <cd> 
        <title>Stop</title>  
        <artist>Sam Brown</artist>  
        <country>UK</country>  
        <company>A and M</company>  
        <price>8.90</price>  
        <year>1988</year> 
      </cd>  
      <cd> 
        <title>Bridge of Spies</title>  
        <artist>T`Pau</artist>  
        <country>UK</country>  
        <company>Siren</company>  
        <price>7.90</price>  
        <year>1987</year> 
      </cd>  
      <cd> 
        <title>Private Dancer</title>  
        <artist>Tina Turner</artist>  
        <country>UK</country>  
        <company>Capitol</company>  
        <price>8.90</price>  
        <year>1983</year> 
      </cd>  
      <cd> 
        <title>Midt om natten</title>  
        <artist>Kim Larsen</artist>  
        <country>EU</country>  
        <company>Medley</company>  
        <price>7.80</price>  
        <year>1983</year> 
      </cd>  
      <cd> 
        <title>Pavarotti Gala Concert</title>  
        <artist>Luciano Pavarotti</artist>  
        <country>UK</country>  
        <company>DECCA</company>  
        <price>9.90</price>  
        <year>1991</year> 
      </cd>  
      <cd> 
        <title>The dock of the bay</title>  
        <artist>Otis Redding</artist>  
        <country>USA</country>  
        <company>Atlantic</company>  
        <price>7.90</price>  
        <year>1987</year> 
      </cd>  
      <cd> 
        <title>Picture book</title>  
        <artist>Simply Red</artist>  
        <country>EU</country>  
        <company>Elektra</company>  
        <price>7.20</price>  
        <year>1985</year> 
      </cd>  
      <cd> 
        <title>Red</title>  
        <artist>The Communards</artist>  
        <country>UK</country>  
        <company>London</company>  
        <price>7.80</price>  
        <year>1987</year> 
      </cd>  
      <cd> 
        <title>Unchain my heart</title>  
        <artist>Joe Cocker</artist>  
        <country>USA</country>  
        <company>EMI</company>  
        <price>8.20</price>  
        <year>1987</year> 
      </cd> 
    </catalog>
    catalog.xml

    XSL文档:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <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>  
              <!-- 可以加上过滤运算符 =(等于) !=(不等于) &lt;(小于) &gt;(大于) 例如:<xsl:for-each select="catalog/cd[artist &lt; 'Bob Dylan']"> -->  
              <xsl:for-each select="catalog/cd"> 
                <!-- 排序 -->  
                <xsl:sort select="artist"/>  
                <xsl:if test="price &gt; 8"> 
                  <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:if> 
              </xsl:for-each> 
            </table> 
          </body> 
        </html> 
      </xsl:template> 
    </xsl:stylesheet>
    catalog01.xsl
    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">  
      <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>
    catalog02.xsl

    把 XSL 样式表链接到 XML 文档

    在 XML 文档开头加上对应的 XSL 文档链接

    <?xml-stylesheet type="text/xsl" href="xsl文档路径"?>

    如下:

    <?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet type="text/xsl" href="catalog01.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>
    catalog.xml
    只需要修改使用不同的 XLS 文档,即可变换 XML 文档的展示样式。
  • 相关阅读:
    《浪潮之巅》笔记之五
    《浪潮之巅》笔记之四
    《浪潮之巅》笔记之三
    《浪潮之巅》笔记之二
    《浪潮之巅》笔记之一
    basename、dirname、alias、date
    grep命令
    centos6下通用二进制格式安装MySQL过程
    在centos6中编译安装httpd-2.4/搭建LAMP
    在服务器端对sshd做白名单
  • 原文地址:https://www.cnblogs.com/alfredinchange/p/5705667.html
Copyright © 2011-2022 走看看