Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

xml - How to create a boolean value?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[4] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

56.8k users

...