xml 如何创建布尔值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/346226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:14:27  来源:igfitidea点击:

How to create a boolean value?

xmlxsltbooleanxslt-1.0xslt-2.0

提问by sydlawrence

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

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

<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?

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

回答by Dimitre Novatchev

The value of the $var variable as defined in:

$var 变量的值定义在:

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

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

is

   true()

   true()

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

这是因为在 XPath 中,“ false” 是一个普通字符串,而不是false(),它是boolean值的构造函数false()

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

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

   true()and false()

   true()false()

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

XPath Spec 中详细说明了将任何值转换为布尔值的细节:

"The booleanfunction 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 "

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

  • 节点集为真当且仅当它不为空

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

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

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().

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

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

因此,要在 XSLT 1.0 中定义一个变量,其值为false(),需要将定义写成如下:

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

   <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"/>

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

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

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

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

在 XSLT 2.0 中,最好明确指定变量的类型

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

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

回答by Yuval Adam

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:

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

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

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

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

  • 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.
  • 如果参数为负数或正数,则将其转换为布尔值 true。
  • 如果参数为零或 NaN 值,则将其转换为 false。
  • 如果参数是非空节点集,则将其转换为 true。一个空的节点集被转换为 false。
  • 如果参数是非空字符串,则将其转换为 true。空字符串被转换为 false。
  • 如果参数是四种基本类型以外的类型的对象,则它会以依赖于该类型的方式转换为布尔值。

回答by outofcoolnames

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:

在这个阶段可能有点晚了,但是处理布尔值的 imo 不值得付出努力。下面是我如何处理从数据库返回的布尔值(强制):

<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

希望这有助于某人