2011-06-09 84 views
2

此代码不能分配一个参数是给我的输出test当预期输出应该是什么..XSLT用布尔值内``<xsl:choose>

是不是有毛病我XSLT处理器或..? :

<xsl:template match="/"> 

     <xsl:param name="IsTextArea"> 
    <xsl:choose> 
     <xsl:when test="false()"> 
      <xsl:value-of select="true()"/> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="false()"/> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:param> 

    <html> 

    <xsl:choose> 
    <xsl:when test="$IsTextArea">test 
    </xsl:when> 
    </xsl:choose> 


    </html> 
    </xsl:template> 

顺便说一句我需要(如,没有扩展和东西)为原料XSLT 1.0的溶液。

是否可以在XSLT 1.0中为param设置布尔参数?

回答

3

您的参数正在评估为一个字符串。您应该使用:

<html> 
     <xsl:choose> 
      <xsl:when test="$IsTextArea = 'true'"> 
       test 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="$IsTextArea"/> 
      </xsl:otherwise> 
     </xsl:choose> 
    </html> 
+0

顺便说一句,如果我将它设置为“1”和“0”,并使用“$ IsTextArea = 1”,它可以吗? – Pacerier 2011-06-09 16:41:27

+0

不可以。正如@Michael-Key正确指出的那样,你总是会有一个字符串。尝试一下 :)! – 2011-06-09 19:22:59

0

它不应该是这样的:

<xsl:param name="IsTextArea"> 
<xsl:choose> 
    <xsl:when test="false()"> 
     <xsl:value-of select="false()"/> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:value-of select="true()"/> 
    </xsl:otherwise> 
</xsl:choose> 

是否认为你的真假是格格不入。

+0

nop他们不是不合适的地方。因为测试是'false()'IsTextArea应该是'false()'然后(请参阅我的问题中的代码)。并且检查应该失败。然而,它成功了,并且自从显示“测试”以来已经验证。 – Pacerier 2011-06-09 16:35:07

2

xsl:value-of指令会将您给它的任何内容转换为字符串。所以true()变成“true”,而false()变成“false”。在布尔上下文中使用字符串时,任何非空字符串变为true(),而“”变为false()。所以boolean-> string->布尔转换不会往返。在XSLT 2.0中,答案是使用xsl:sequence而不是xsl:value-of;在1.0中,只需使用“是”和“否”等字符串以避免混淆。

+1

对不起,你能详细说明'xsl:sequence'部分吗?你的意思是说,我们将设置一个序列为1个布尔值的项目? – Pacerier 2011-06-09 16:36:40

+0

+1的建议。微妙之处,微妙之处...... :-) – GuruM 2013-02-13 16:18:57