懒码农。。。。。。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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 属性来规定子节点被处理的顺序。


 

 

 

 

posted on 2012-02-14 15:43  阿彬  阅读(314)  评论(0编辑  收藏  举报