[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Nuances of comparisons incorporating node set operands

Subject: [xsl] Nuances of comparisons incorporating node set operands
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 01 Apr 2004 22:56:48 -0500

At 2004-04-02 06:33 +0300, Jarno.Elovirta@xxxxxxxxx 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"

<xsl:output method="text"/>



<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 select="document('')/*/jarno:data2">
Data test 2:
    test="Col3 != ''"     - <xsl:value-of select="Col3 != ''"/>
    test="not(Col3 = '')" - <xsl:value-of select="not(Col3 = '')"/>

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

-- 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@xxxxxxxxxxxxxxxxxxxx
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

Current Thread