如何创建布尔值?

发布于 2024-07-10 01:59:38 字数 214 浏览 8 评论 0原文

我对 XSLT 完全陌生,无法弄清楚以下代码哪里出了问题。

<xsl:variable name="var" select="boolean('false')"/>

<xsl:if test="$var'">variable is true</xsl:if>

当它本应为假时,它总是返回真。 为什么?

I am totally new to XSLT and can't work out where I am going wrong with the following code.

<xsl:variable name="var" select="boolean('false')"/>

<xsl:if test="$var'">variable is true</xsl:if>

It is always returning true when it is meant to be false. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东京女 2024-07-17 01:59:38

$var 变量的值定义如下:

   

    true()

这是因为在 XPath 中“false”是一个普通字符串,与 false() 相反,它是 booleanfalse() 的构造函数

XPath 中的两个布尔值是(注意它们是构造的!):

   true()false()

转换任何值的详细信息布尔值在 XPath 规范 中详细说明:

boolean 函数将其参数转换为布尔值,如下所示:

  • 一个数字为真当且仅当
    既不是正零也不是负零
    也不是 NaN

  • 节点集为真当且仅当
    非空

  • 字符串为真当且仅当其
    长度非零

  • 四种基本类型以外的类型的对象以依赖于该类型的方式转换为布尔值
    "

在您的情况下,字符串“false”不是数字 0 并且具有正长度,因此应用上面第三个项目符号中的规则,产生 true()

因此,要定义XSLT 1.0 中的变量,其值为 false( ),定义如下:

   

或者,如果您不太记得这一点,您可以随时编写:

   

(指定任何计算结果为 false() 的表达式),XSLT 处理器将为您完成工作

在 XSLT 2.0 中,最好显式指定类型 。变量的

The value of the $var variable as defined in:

   <xsl:variable name="var" select="boolean('false')"/>

is

   true()

This is because in XPath "false" is an ordinary string, as opposed to false(), which is the constructor for the boolean value false()

The two boolean values in XPath are (note that they are constructed!):

   true() and false()

The detail of converting any value to boolean are spelled outin the XPath Spec.:

"The boolean function converts its argument to a boolean as follows:

  • a number is true if and only if it
    is neither positive or negative zero
    nor NaN

  • a node-set is true if and only if it
    is non-empty

  • a string is true if and only if its
    length is non-zero

  • an object of a type other than the four basic types is converted to a boolean in a way that is dependent on that type
    "

In your case the string "false" is not the number 0 and has a positive length, so the rule in the 3rd bullet above is applied, yielding true().

Therefore, to define a variable in XSLT 1.0, whose value is false(), one needs to write the definition as the following:

   <xsl:variable name="vMyVar" select="false()"/>

or, if you don't exactly remember this, you could always write:

   <xsl:variable name="vMyVar" select="1 = 0"/>

(specify any expression that evaluates to false()) and the XSLT processor will do the work for you.

In XSLT 2.0 it is always better to explicitly specify the type of the variable:

   <xsl:variable name="vMyVar" as="xs:boolean" select="false()"/>

如梦初醒的夏天 2024-07-17 01:59:38

也许在这个阶段有点晚了,但在我看来,处理布尔值是不值得的。 这是我处理从数据库返回的布尔值(强制)的方式:

<xsl:variable name="vTrue" select="true()"/>                     
    <xsl:choose>
      <xsl:when test="string(Mandatory) = string($vTrue)">
        <xsl:text>Mandatory</xsl:text>
      </xsl:when>
      <xsl:otherwise>           
      </xsl:otherwise>
    </xsl:choose>

希望这对某人有帮助

A bit late at this stage perhaps but imo dealing with booleans is just not worth the effort. Heres how I dealt with a boolean (Mandatory) coming back from the DB:

<xsl:variable name="vTrue" select="true()"/>                     
    <xsl:choose>
      <xsl:when test="string(Mandatory) = string($vTrue)">
        <xsl:text>Mandatory</xsl:text>
      </xsl:when>
      <xsl:otherwise>           
      </xsl:otherwise>
    </xsl:choose>

Hope this helps someone

遗失的美好 2024-07-17 01:59:38

您正在使用的 boolean() 函数确实在完成它的工作。 要使用明确的 true 和 false 值,您应该使用以下函数:

<xsl:variable name="var_false" select="false()"/>
<xsl:variable name="var_true" select="true()"/>

仅供参考,根据 MSDN 文档,boolean() 返回以下内容:

  • 如果参数是负数或正数,则将其转换为布尔值 true。
  • 如果参数为零或 NaN 值,则将其转换为 false。
  • 如果参数是非空节点集,则将其转换为 true。 空节点集将转换为 false。
  • 如果参数是非空字符串,则将其转换为 true。 空字符串将转换为 false。
  • 如果参数是四种基本类型以外的类型的对象,则会以依赖于该类型的方式将其转换为布尔值。

The boolean() function you are using is indeed doing it's job. For using explicit true and false values you should use the following functions:

<xsl:variable name="var_false" select="false()"/>
<xsl:variable name="var_true" select="true()"/>

Just FYI, per the MSDN documentation, boolean() returns the following:

  • If the argument is a negative or positive number, it is converted to the Boolean value true.
  • If the argument is zero or an NaN value, it is converted to false.
  • If the argument is a non-empty node-set, it is converted to true. An empty node-set is converted to false.
  • If the argument is a non-empty string, it is converted to true. An empty string is converted to false.
  • If the argument is an object of a type other than the four basic types, it is converted to a Boolean in a way that is dependent on that type.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文