Re: [xsl] String comparisons fail unexpectedly: how can 'USA' not be 'USA'?

Subject: Re: [xsl] String comparisons fail unexpectedly: how can 'USA' not be 'USA'?
From: "Thomas B. Passin" <tpassin@xxxxxxxxxxxx>
Date: Wed, 27 Feb 2002 10:30:43 -0500

> this is very frustrating! I want to pull out lots of records from a data
> source using a list. The data source has records that look like this:
> <Billcode corp="EvilEmpire" country="USA">
> and the list is an html table with rows like this:
> <tr><td>EvilEmpire</td><td>USA</td></tr>
> Initially I tried using a key with use="concat(@corp,'::',@country)" but
> joy, after a lot of trying things out very patiently or not so patiently I
> tried this:
> <xsl:value-of select="Billcode[2]/@country"/>
> <xsl:value-of select="'USA'=Billcode[2]/@country"/>
> and got, to my surprise, "USAfalse".
> There must be something important I don't know about string comparisons!

There are several things involved here.  First,


is asking whether the string "USA' equals the value of a node;  that's what
you get a "false" (or "true"), rather than a string value.

Second, string values often contain leading or trailing whitespace, which
may be there by accident or for visual formatting.   You can get rid of the
extra whitespace by using normalize-space():


Finally, a the "use" parameter for a key is to be a path expression, not a
string.  You may be able to make use of a string by making it part of a path
expression, like this:


But you can't build a string that __looks__ like a path expression and have
that work.  In fact, this won't be the best approach anyway, since you
wouldnt know what values to specify when you define the key - unless you
used a 2-step approach, where the first stylesheet build a second one that
does the real work.

To make use of the values from your list, you could define a key using

use=' Billcode/@corp'

and then use the values from the html list to get your nodes using the key:

<xsl:variable name='currentcorp' select='Billcode[2]/@corp'/>
<xsl:variable name='currentcountry' select='Billcode[2]/@country'/>

<xsl:value-of select='key("corp-key",$currentcorp)'/>
<xsl:value-of select='key("country-key",$currentcountry)'/>

You can save the returns from each key in a variable instead, and then use
them in logic tests or whatever you want.  The details are probably wrong
because I'm not working with the real xml structures that you have, but you
should be able to see the main ideas.

You may be able to combine some of these pieces, but I've shown then
separately so you can see how they work.


Tom P

 XSL-List info and archive:

Current Thread