zoukankan      html  css  js  c++  java
  • XSLT教程

    XPATH:http://www.w3school.com.cn/xpath/xpath_syntax.asp 

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

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

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

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

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

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

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

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

    <xsl:if>元素

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

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

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

    <xsl:choose>

    <xsl:when test="expression">

    ... 输出 ...

    </xsl:when>

    <xsl:otherwise>

    ... 输出 ....

    </xsl:otherwise>

    </xsl:choose>

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

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

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


  • 相关阅读:
    一段自己写的丑陋的表单验证代码
    简单的星级评价
    有个项目
    好久没写了,重装了系统重新配置的Live Writer,看看效果:
    XmlHttpRequest调用Webservice的一点心得
    局域网共享怎么设置?我想把其中一个电脑的F盘共享?
    TCP/IP协议详解
    input file实现多张图片上传
    .NET C#中如何备份SQL数据库
    CSS中cursor鼠标形状属性列表
  • 原文地址:https://www.cnblogs.com/abinxm/p/2351178.html
Copyright © 2011-2022 走看看