您的位置:首页 > Web前端 > HTML

XSLT实例

2005-04-11 00:18 302 查看

1 复制

这是个复制所有节点的例子(程序1)。例如从XHTML变换成XHTML等情况,源和结果的元素大部分相同时,就可以将该实例稍作修改,仅针对需要改动的部分书写代码即可。 程序1: copy.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/">
      <xsl:apply-templates/>
   </xsl:template>
   <xsl:template match="@*|node()">
     <xsl:copy>
       <xsl:apply-templates select="@*|node()"/>
     </xsl:copy>
   </xsl:template>
</xsl:stylesheet>
使用xsl:xcopy元素将各节点进行递归复制。

2 行列交换

该实例可以将HTML表格的行和列进行交换(程序2)。
程序2: table.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"/>
    <xsl:template match="/">
       <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="table">
       <xsl:copy>
         <xsl:apply-templates select="@*"/>
         <xsl:for-each select="tr[1]/td">
           <tr>
             <xsl:variable name="x" select="last()"/>
             <xsl:variable name="y" select="position()-1"/>
             <xsl:for-each select="../../tr/td">
                <xsl:if test="(position()-1) mod $x = $y">
                   <td>
                     <xsl:value-of select="."/>
                   </td>
                </xsl:if>
            </xsl:for-each>
          </tr>
        </xsl:for-each>
      </xsl:copy>
    </xsl:template>
    <xsl:template match="@*|node()">
       <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
       </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
因为是交换行列,所以只需输出源表格的列数的行就可以了。在这里,使用
<xsl:for-each select="tr[1]/td">
循环,按照第一行的列数来新建行(tr元素)。使用如下循环
<xsl:for-each select="../../tr/td">
来取出各行中将要保存的数据。由于当前节点是tr[1]/td,那么使用../tdと将仅能取出第一行的td元素。为了取出所有td元素,须写成../../tr/td的形式。 变量x中保存新建表格的总行数,变量y中保存当前正在生成的行的行数-1。因此,
<xsl:if test="(position()-1) mod $x = $y">
使用该条件输出td元素中的数据。mod是取余数的运算符。
将如图1所示的页面(程序3)进行转换,可以得到如图2所示的输出结果。


[align=center]图1: 输入页面[/align]程序3: table.xml
<?xml version="1.0"?>
   <html>
     <head>
       <title>表格</title>
       <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
   </head>
<h1>表格</h1>
<body>
  <table border="1">
     <tr>
        <td>甲</td>
         <td>乙</td> 
        <td>丙</td> 
        <td>丁</td> 
        <td>戊</td> 
    </tr>
    <tr> 
        <td>己</td> 
        <td>庚</td> 
        <td>辛</td> 
        <td>壬</td> 
        <td>癸</td>
  </tr>
</table>
</body>
</html>


[align=center]图2: 输出页面[/align]

3 表格

表格可以看作是二维数组。让我们试着将它转换成一维数组。但是仅仅将表格内容排列出来就失去了意义,因此我们将保留原表格的行号和列号作为结果的属性值(程序4)。
程序4: databank.xsl
<?xml version="1.0"?>
  <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
      <xsl:template match="/"> 
        <databank>
           <xsl:apply-templates select=".//table"/> 
        </databank> 
      </xsl:template> 
      <xsl:template match="table"> 
        <xsl:for-each select="tr"> 
          <xsl:variable name="row" select="position()"/> 
          <xsl:for-each select="td"> 
            <xsl:variable name="column" select="position()"/> 
            <data> 
              <xsl:attribute name="row"> 
                <xsl:copy-of select="$row"/>
              </xsl:attribute> 
              <xsl:attribute name="column"> 
                <xsl:copy-of select="$column"/> 
              </xsl:attribute>
               <xsl:value-of select="."/> 
            </data>
         </xsl:for-each> 
      </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>
将行号和列号的信息保存到变量row和column中。
<xsl:variable name="row" select="position()"/>
<xsl:variable name="column"select="position()"/>
使用row属性输出行号,column属性输出列号。
<xsl:attribute name="row"> 
  <xsl:copy-of select="$row"/>
</xsl:attribute>
<xsl:attribute name="column"> 
  <xsl:copy-of select="$column"/>
</xsl:attribute>
将程序3进行转换时,输出如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<databank> 
  <data row="1" column="1">甲</data> 
  <data row="1" column="2">乙</data>
   <data row="1" column="3">丙</data> 
  <data row="1" column="4">丁</data> 
  <data row="1" column="5">戊</data>
  <data row="2" column="1">己</data> 
  <data row="2" column="2">庚</data> 
  <data row="2" column="3">辛</data> 
  <data row="2" column="4">壬</data> 
  <data row="2" column="5">癸</data>
</databank>

4 将表格转换为CSV文件

虽然XSLT一般用来将XML文档转换成XML文档,但是也能够输出XML文档之外的格式。下面是将含有table元素的HTML文档转换成CSV(Comma Separated Value)格式1输出的例子(程序5)。
程序5: csv.xsl
<?xml version="1.0"?> 
  <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text" encoding="UTF-8"/>
    <xsl:template match="/">
       <xsl:apply-templates select=".//tr"/> 
    </xsl:template> 
    <xsl:template match="tr"> 
      <xsl:for-each select="td"> 
        <xsl:value-of select="."/> 
        <xsl:if test="not(position()=last())">
           <xsl:text>,</xsl:text>
        </xsl:if> 
      </xsl:for-each> 
<xsl:text>
</xsl:text> 
    </xsl:template>
</xsl:stylesheet>
为输出文本格式,需要指定
<xsl:output method="text" encoding="UTF-8"/>
由于被转换的对象节点是表示行的tr元素,因此针对tr元素定义模板规则。若不是行中的最后一个元素,则输出分隔符“,”。
<xsl:if test="not(position()=last())"> 
    <xsl:text>,</xsl:text>
</xsl:if>
将程序3进行转换的结果如下所示。
甲,乙,丙,丁,戊
己,庚,辛,壬,癸
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息