2009-10-13 35 views
0

我有以下模板:xslt1.0變量的問題

<xsl:template name="theday"> 
    <xsl:param name="thisday" /> 

    <xsl:variable name='holiday' select='foo'/><!-- made this static for testing --> 

    <td class="{$holiday}"> <!-- no value is inserted in class --> 

     <a> 
      <xsl:attribute name='href'><xsl:value-of 
       select="concat('?date=',$thisday)" /></xsl:attribute> 
      <xsl:value-of select="date:day-in-month($thisday)" /> 
     </a> 
    </td> 
</xsl:template> 

我期望能獲得HTML是這樣的:

<td class="foo"> 
    <a href="?date=2009-11-02">2</a> 
</td> 

不幸的是,我得到:

<td class=""> 
    <a href="?date=2009-11-02">2</a> 
</td> 

什麼我錯過了嗎?

回答

3

試試這個:

<xsl:variable name='holiday'>foo</xsl:variable> 

<xsl:variable name='holiday' select="'foo'"/> 

其工作原理:select屬性期待一個表達進行評估;由於上下文中可能沒有foo元素,因此將其解析爲空字符串。

1

問題是,<xsl:variable name='holiday' select='foo'/>選擇nodelist'foo'(它是空的)而不是字符串foo。如果你有XML

<a> 
    <foo>B</foo> 
</a> 

然後(在目前a<xsl:variable name='holiday' select='foo'/>會給 'B'。

爲了解決這個問題指定一個常數:

<xsl:variable name='holiday' select="'foo'"/>