XSL基础知识:显示布尔值?

ing*_*.am 4 xslt boolean

当我在xsl中有这个:

<xsl:choose>
  <xsl:when test="something > 0">
    <xsl:variable name="myVar" select="true()"/>
  </xsl:when>
  <xsl:otherwise>
    <xsl:variable name="myVar" select="false()"/>
  </xsl:otherwise>
</xsl:choose>
Run Code Online (Sandbox Code Playgroud)

我怎样才能打印出"myVar"的值?或者更重要的是,如何在另一个select语句中使用此布尔值?

Dim*_*hev 7

<xsl:choose>
  <xsl:when test="something > 0">
    <xsl:variable name="myVar" select="true()"/>
  </xsl:when>
  <xsl:otherwise>
    <xsl:variable name="myVar" select="false()"/>
  </xsl:otherwise>
</xsl:choose>
Run Code Online (Sandbox Code Playgroud)

这是非常错误和无用的,因为变量$myVar立即超出范围.

有条件地分配给变量的一种正确方法是:

<xsl:variable name="myVar">
  <xsl:choose>
    <xsl:when test="something > 0">1</xsl:when>
    <xsl:otherwise>0</xsl:otherwise>
  </xsl:choose>
</xsl:variable>
Run Code Online (Sandbox Code Playgroud)

但是,你真的不需要这个 - 更简单的是:

<xsl:variable name="myVar" select="something > 0"/>
Run Code Online (Sandbox Code Playgroud)
How can I then print out the value of "myVar"?
Run Code Online (Sandbox Code Playgroud)

用途:

<xsl:value-of select="$myVar"/>
Run Code Online (Sandbox Code Playgroud)

或者更重要的是,如何在另一个select语句中使用此布尔值?

这是一个简单的例子:

<xsl:choose>
  <xsl:when test="$myVar">
   <!-- Do something -->
  </xsl:when>
  <xsl:otherwise>
   <!-- Do something else -->
  </xsl:otherwise>
</xsl:choose>
Run Code Online (Sandbox Code Playgroud)

这是一个完整的例子:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*/*">
  <xsl:variable name="vNonNegative" select=". >= 0"/>

  <xsl:value-of select="name()"/>: <xsl:text/>

  <xsl:choose>
   <xsl:when test="$vNonNegative">Above zero</xsl:when>
   <xsl:otherwise>Below zero</xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

当此转换应用于以下XML文档时:

<temps>
 <Monday>-2</Monday>
 <Tuesday>3</Tuesday>
</temps>
Run Code Online (Sandbox Code Playgroud)

产生了想要的正确结果:

 Monday: Below zero
 Tuesday: Above zero
Run Code Online (Sandbox Code Playgroud)