2016-03-03 48 views
1

這裏是我的XSLT: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,因爲它工作正常,如果我硬編碼一個值。

對此提出建議?

+0

貌似你試圖[執行動態的XPath(http://stackoverflow.com/questions/4630023/dynamic-xpath-in-xslt)? – har07

+1

@ har07,不,它沒有那麼複雜 - 只是[範圍界定問題](http://stackoverflow.com/a/35763135/290085)。 – kjhughes

回答

0

變量,pageurl,內xsl:otherwisexsl:when和(再次)定義的,超出範圍由您嘗試使用它作爲一個參數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>