Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


[xsl] Nuances of comparisons incorporating node set operands

From: "G. Ken Holman" <gkholman@-------------------->
To:
Date: 4/1/2004 5:59:00 PM
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"
                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@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


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent