现在的位置: 首页 > 综合 > 正文

xslt variable标签 学习笔记及使用技巧

2013年01月22日 ⁄ 综合 ⁄ 共 704字 ⁄ 字号 评论关闭
  1. 顶层变量:全局的,定义在样式表的顶层 ,在任何模板之外。顶层变量在整个样式表都是可见的,甚至出现于变量声名的模板中。
  2. 定义变量的其他地方是在模板内。这些变量只是对模板内跟随在<xsl:variable>声名后的元素及其后代元素是可见的。
  3. 使用变量的主要限制就是不能修改变量。例如在一个<xsl:for-each>循环中使用一个变量作为计数器是不可能的。

    但是可以利用一些技巧来克服这一点,例如:

    <xsl:variable name="midName">
        <xsl:choose>
               <xsl:when test="middleName">
                     <xsl:value-of select = "middleName"/>
               </xsl:when>
               <xsl:otherwise>aaa</xsl:otherwise>
        </choose>
    </xsl:variable>

    上例将变量定义作为<xsl:variable>的一个子元素。
    Tip:通常可以将一个参数传递到模板而不是使用全局变量,然后再使用一个增量参数值递归调用这个模板。

  4. 三种定义的方法:
    <xsl:variable name="homepage">index.html</xsl:variable>
    <xsl:variable name="last" select = "president[position()=last()]/name"/>
    <xsl:variable name="empty" select = " '' "
  5. 引用变量时在变量名前加$
    例如<xsl:value-of select = "$last"/>

抱歉!评论已关闭.