xsl-list
[Top] [All Lists]

Re: Finding and processing index terms

2006-02-15 10:45:14

This thread shows the danger of using !=

Basically it's best avoided unless both sides of the expression are
atomic values not node sets.

If $a and $b are both atomic values then 
$a != $b
means the same as
not($a = $b)
however when one (or especially) both $a or $b are node sets these mean
very different things, and almost always you want not($a = $b) rather
than $a != $b. So I usually find that rather than worry if it's safe to
use != it's simpler just to always use not( .. = ...).

To see the difference, consider
<a>
<x a="n"/>
<x/>
</a>



Then ask for all the children of x that don't have an attribute that is 'y'
(or the different question, have an a attribute that is not 'y')

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:template match="a">
a: <xsl:value-of select="count(*[(_at_)a!='y'])"/>
b: <xsl:value-of select="count(*[not(@a='y')])"/>
c: <xsl:value-of select="count(*[string(@a)!='y'])"/>
</xsl:template>

</xsl:stylesheet>

$ saxon neq.xml neq.xsl
<?xml version="1.0" encoding="utf-8"?>
a: 1
b: 2
c: 2



If you had used either form (b) or (c) your answer would not have
depended on the attribute being defaulted, as no attribute would work
the same way as  publicOnly="" which would result in the string "" being
compared with 'yes' and both '' and 'No' are not equal to yes.
But the first form, using != filters out all elements with no attribute.

David

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--