【问题标题】:Variable scope in XSLT?XSLT 中的变量范围?
【发布时间】:2016-03-03 03:11:46
【问题描述】:

这是我的 XSLT:

<xsl:choose>  
  <xsl:when test="string-length(/*/location/name)">
    <xsl:variable name="pagurl">/location/<xsl:value-of select="/*/location/@id" />comments</xsl:variable>
  </xsl:when>
  <xsl:otherwise>
    <xsl:variable name="pagurl">/state/<xsl:value-of select="/*/state/@code" />/comments</xsl:variable>
  </xsl:otherwise>
</xsl:choose>

<div class="pagination_outer" id="pager">
  <xsl:call-template name="pagination">
    <xsl:with-param name="url"><xsl:value-of select="$pagurl"/></xsl:with-param>                                
  </xsl:call-template>
</div>

我正在做以下事情:

  1. 将变量$pagurl 分配给基于字符串长度的值。
  2. 试图在调用<xsl:with-param name="url"><xsl:value-of select="$pagurl"/></xsl:with-param>中使用变量

当我包含这个调用时,页面似乎永远不会完成加载,但是当我不使用它时,页面加载得很好。 我想我的用法有误。

我认为没有必要查看pagination template,因为如果我硬编码一个值,它就可以正常工作。

建议?

【问题讨论】:

标签: xml xslt


【解决方案1】:

xsl:when 和(再次)在xsl:otherwise 中定义的变量pageurl 在您尝试将其用作pagination 的参数时已超出范围。你说页面似乎永远无法完成加载,但我怀疑你错过了一条错误消息。

试试这个:

<xsl:variable name="pagurl">
  <xsl:choose>  
    <xsl:when test="string-length(/*/location/name)">
      <xsl:text>/location/</xsl:text>
      <xsl:value-of select="/*/location/@id"/>
      <xsl:text>comments</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>/state/</xsl:text>
      <xsl:value-of select="/*/state/@code"/>
      <xsl:text>/comments</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<div class="pagination_outer" id="pager">
  <xsl:call-template name="pagination">
    <xsl:with-param name="url" select="$pagurl"/>
  </xsl:call-template>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-10
    • 2021-11-14
    相关资源
    最近更新 更多