Subject: RE: Boolean true() and false() as strings? From: "Evan Lenz" <elenz@xxxxxxxxxxx> Date: Thu, 5 Oct 2000 17:16:28 -0700 |
P.S. For all practical purposes, a Result Tree Fragment containing just text will look and act the same as an XPath string containing that text, except that an RTF with an empty string value will still convert to boolean true, just like a node-set containing one root, which is what it will be in the future after XSLT 1.1 rids itself of the confusing RTF data type. Here's the entire section covering Result Tree Fragments from the XSLT spec. It's fairly concise and could potentially clear up some of the confusion. <snip href="http://www.w3.org/TR/xslt#section-Result-Tree-Fragments"> 11.1 Result Tree Fragments Variables introduce an additional data-type into the expression language. This additional data type is called result tree fragment. A variable may be bound to a result tree fragment instead of one of the four basic XPath data-types (string, number, boolean, node-set). A result tree fragment represents a fragment of the result tree. A result tree fragment is treated equivalently to a node-set that contains just a single root node. However, the operations permitted on a result tree fragment are a subset of those permitted on a node-set. An operation is permitted on a result tree fragment only if that operation would be permitted on a string (the operation on the string may involve first converting the string to a number or boolean). In particular, it is not permitted to use the /, //, and [] operators on result tree fragments. When a permitted operation is performed on a result tree fragment, it is performed exactly as it would be on the equivalent node-set. When a result tree fragment is copied into the result tree (see [11.3 Using Values of Variables and Parameters with xsl:copy-of]), then all the nodes that are children of the root node in the equivalent node-set are added in sequence to the result tree. Expressions can only return values of type result tree fragment by referencing variables of type result tree fragment or calling extension functions that return a result tree fragment or getting a system property whose value is a result tree fragment. </snip> Evan Lenz elenz@xxxxxxxxxxx http://www.xyzfind.com XYZFind, the search engine *designed* for XML Download our free beta software: http://www.xyzfind.com/beta -----Original Message----- From: Evan Lenz [mailto:elenz@xxxxxxxxxxx] Sent: Thursday, October 05, 2000 5:01 PM To: xsl-list@xxxxxxxxxxxxxxxx Subject: RE: Boolean true() and false() as strings? Ah yes, boolean conversion of result tree fragments. The first thing you need to know to explain the behavior of your stylesheet is that whenever an <xsl:variable> element is not empty, it will always return a value of type Result Tree Fragment. More to the point, <xsl:value-of/> always copies the *string-value* of the designated XPath expression, boolean or otherwise. Try hard-coding the true() value, first as a child of <xsl:variable> and then as the value of the select attribute. This returns an RTF: <xsl:variable name="units_gt_300"> <xsl:value-of select="true()"/> </xsl:variable> This returns an XPath boolean: <xsl:variable name="units_gt_300" select="true()"/> Your if test will evaluate differently depending on the variable's type. In the first case, the comparison is between an RTF and a string. The processor first converts the RTF to a string and subsequently performs a string comparison. Thus, you are effectively comparing 'true' with 'true' or 'false' with 'true'. If you changed the if test to "$units_gt_300" or "$units_gt_300=true()" (same thing), it would not work correctly, because 'true' and 'false' will always evaluate to true, being non-empty strings. In the second case, the comparison is between a boolean and a string. The string is first converted to a boolean and then the comparison is made. If the string you include is not empty, then it will always convert to true, which effectively gives you the same result as "$units_gt_300" or "$units_gt_300='anything'". This behavior is described in the XPath spec as follows: "If at least one object to be compared is a boolean, then each object to be compared is converted to a boolean as if by applying the boolean function." -- http://www.w3.org/TR/xpath#booleans Hope this helps! You're keeping me on my toes ;) Evan Lenz elenz@xxxxxxxxxxx http://www.xyzfind.com XYZFind, the search engine *designed* for XML Download our free beta software: http://www.xyzfind.com/beta -----Original Message----- From: owner-xsl-list@xxxxxxxxxxxxxxxx [mailto:owner-xsl-list@xxxxxxxxxxxxxxxx]On Behalf Of John E. Simpson Sent: Thursday, October 05, 2000 1:18 PM To: xsl-list@xxxxxxxxxxxxxxxx Subject: Boolean true() and false() as strings? A curious thing (well, to me!)... consider this template rule: <xsl:template match="sales"> <h2>Regions Selling More than 300 Units:</h2> <xsl:for-each select="region"> <xsl:variable name="units_gt_300"> <xsl:choose> <xsl:when test="number(units) > 300"> <xsl:value-of select="true()"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="false()"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:if test="$units_gt_300='true'"> <h3><xsl:value-of select="concat(@name, ' (', units, ' units)')"/></h3> </xsl:if> </xsl:for-each> </xsl:template> Note the assignment of Boolean true() or false() to the variable, and the <xsl:if> test. Why can't the latter be simply: <xsl:if test="$units_gt_300"> i.e., why the need to test vs. the string value "true"? Does using the true() and false() functions in a variable coerce their return values to string type rather than their "native" Boolean? Here's a simple XML doc to run it against: <sales quarter="2001-01"> <region name="Northeast"> <units>374</units> <amount>12500.26</amount> </region> <region name="Southeast"> <units>512</units> <amount>17692</amount> </region> <region name="Southwest"> <units>161</units> <amount>8349.72</amount> </region> <region name="Northwest"> <units>465</units> <amount>15239.6</amount> </region> </sales> (Tested with both Saxon and the Sept. release of IE5.5, btw.) ========================================================== John E. Simpson | "Curiosity killed the cat, http://www.flixml.org | but for a while I was a XML Q&A: http://www.xml.com | suspect." (Steven Wright) XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: Boolean true() and false() as s, Evan Lenz | Thread | RE: Boolean true() and false() as s, Kay Michael |
RE: Boolean true() and false() as s, Evan Lenz | Date | RE: XSLT and Default namespaces, Evan Lenz |
Month |