xsl-list
[Top] [All Lists]

Nuances of comparisons incorporating node set operands

2004-04-01 20:56:48
At 2004-04-02 06:33 +0300, Jarno(_dot_)Elovirta(_at_)nokia(_dot_)com wrote:
> <xsl:for-each select="Reports/GridBody0">
>
>      <xsl:if test="Col3 != ''" >

Though correct, it's preferable to use

  not(Col3 = '')

Note that the above two expressions do not always produce the same results, though many of my XSL students over the years have assumed that they are identical and are surprised to learn the nuances.

If a Col3 element child exists for the current node, they are identical.

If a Col3 element child does not exist for the current node, the first expression will produce a false and the second expression will produce a true.

Many forget that a comparison of a node set to another operand is initialized to false and if there are no members of the node set the false value is returned. The use of not() in the expression above changes the test to true. An illustration is below.

I hope this is considered helpful, though it is off topic from the original post. I was worried someone might read the above in the context of their own transformation problem and assume they were identical.

.................... Ken

T:\ftemp>type jarno.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:jarno="test"
                version="1.0">

<xsl:output method="text"/>

<jarno:data1>
  <Col1>abc</Col1>
  <Col3>def</Col3>
</jarno:data1>

<jarno:data2>
  <Col1>abc</Col1>
</jarno:data2>

<xsl:template match="/">
  <xsl:for-each select="document('')/*/jarno:data1">
Data test 1:
    test="Col3 != ''"     - <xsl:value-of select="Col3 != ''"/>
    test="not(Col3 = '')" - <xsl:value-of select="not(Col3 = '')"/>
  </xsl:for-each>
  <xsl:for-each select="document('')/*/jarno:data2">
Data test 2:
    test="Col3 != ''"     - <xsl:value-of select="Col3 != ''"/>
    test="not(Col3 = '')" - <xsl:value-of select="not(Col3 = '')"/>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>saxon jarno.xsl jarno.xsl

Data test 1:
    test="Col3 != ''"     - true
    test="not(Col3 = '')" - true
Data test 2:
    test="Col3 != ''"     - false
    test="not(Col3 = '')" - true
T:\ftemp>


--
Public courses: Spring 2004 world tour of hands-on XSL instruction
Each week:   Monday-Wednesday: XSLT/XPath; Thursday-Friday: XSL-FO
Hong Kong May 17-21; Bremen Germany May 24-28; Helsinki June 14-18

World-wide on-site corporate, govt. & user group XML/XSL training.
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Breast Cancer Awareness  http://www.CraneSoftwrights.com/s/bc



<Prev in Thread] Current Thread [Next in Thread>
  • Nuances of comparisons incorporating node set operands, G. Ken Holman <=