首页 > 开发 > Xml > 正文

XSL基础教程第五章

2018-10-16 21:14:45
字体:
来源:转载
供稿:网友
XSL条件IF

XSL可以用一个IF语句过滤来自XML文档的信息。

在哪里放置IF条件

现在来重新看看你已经看过多次的XML文档:
  
以下为引用的内容:
    <?xml version="1.0"?>
  
    <CATALOG>
  
     <CD>
  
      <TITLE>Empire Burlesque</TITLE>
  
      <ARTIST>Bob Dylan</ARTIST>
  
      <COUNTRY>USA</COUNTRY>
  
      <COMPANY>Columbia</COMPANY>
  
      <PRICE>10.90</PRICE>
  
      <YEAR>1985</YEAR>
  
     </CD>
  
    .
  
    .
  
    .
  
要想放置一个对文件内容的条件测试if命令,只需要向XSL文档中增加一个xsl:if元素,如下:
  
以下为引用的内容:
    <xsl:if match=".[ARTIST='Bob Dylan']">
  
    ... 一些输出...
  
    </xsl:if>
  
现在看一下经过轻微调整的XSL样式表:
  
以下为引用的内容:
    <?xml version='1.0'?>
  
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
  
    <xsl:template match="/">
  
     <html>
  
     <body>
  
      <table border="2" bgcolor="yellow">
  
       <tr>
  
        <th>Title</th>
  
        <th>Artist</th>
  
       </tr>
  
       <xsl:for-each select="CATALOG/CD">
  
       <xsl:if match=".[ARTIST='Bob Dylan']">
  
       <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>
  
在浏览器中转换

以下是在浏览器中将XML文件转换成HTML所需要的简单代码
  
以下为引用的内容:
    <html>
  
    <body>
  
    <script language="javascript">
  
    // Load XML
  
    var xml = new ActiveXObject("Microsoft.XMLDOM")
  
    xml.async = false
  
    xml.load("cd_catalog.xml")
  
    // Load the XSL
  
    var xsl = new ActiveXObject("Microsoft.XMLDOM")
  
    xsl.async = false
  
    xsl.load("cd_catalog_if.xsl")
  
    // Transform
  
    document.write(xml.transformNode(xsl))
  
    </script>
  
    </body>
  
    </html>
  
如果使用的是Internet Explorer 5.0 或更高版本,请点击这里查看结果。

XSL条件选择Choose

XSL可以使用条件选择过滤XML文档。

在哪里放置选择条件

重新看看几乎在前面每个章节都看到过的XML文档:
  
以下为引用的内容:
    <?xml version="1.0"?>
  
    <CATALOG>
  
     <CD>
  
      <TITLE>Empire Burlesque</TITLE>
  
      <ARTIST>Bob Dylan</ARTIST>
  
      <COUNTRY>USA</COUNTRY>
  
      <COMPANY>Columbia</COMPANY>
  
      <PRICE>10.90</PRICE>
  
      <YEAR>1985</YEAR>
  
     </CD>
  
    .
  
    .
  
    .
  
要想插入一个对文件内容的条件选择测试,只需要向XSL文档中增加xsl:choose、xsl:when 以及 xsl:otherwise 元素,如下:
  
以下为引用的内容:
    <xsl:choose>
  
     <xsl:when match=".[ARTIST='Bob Dylan']">
  
       ... 一些代码 ...
  
     </xsl:when>
  
     <xsl:otherwise>
  
       ... 一些代码 ...
  
     </xsl:otherwise>
  
    </xsl:choose>
  
现在来看看经过轻微调整的XSL样式表:
  
以下为引用的内容:
    <?xml version='1.0'?>
  
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
  
    <xsl:template match="/">
  
     <html>
  
     <body>
  
      <table border="2" bgcolor="yellow">
  
       <tr>
  
        <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 match=".[ARTIST='Bob Dylan']">
  
          <td bgcolor="#ff0000">
  
           <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>
  
在浏览器中转换

以下是在浏览器中将XML文件转换成HTML所需要的简单代码
  
以下为引用的内容:
    <html>
  
    <body>
  
    <script language="javascript">
  
    // Load XML
  
    var xml = new ActiveXObject("Microsoft.XMLDOM")
  
    xml.async = false
  
    xml.load("cd_catalog.xml")
  
    // Load the XSL
  
    var xsl = new ActiveXObject("Microsoft.XMLDOM")
  
    xsl.async = false
  
    xsl.load("cd_catalog_choose.xsl")
  
    // Transform
  
    document.write(xml.transformNode(xsl))
  
    </script>
  
    </body>
  
    </html>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表