This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


[<TSchutzerWeissmann@uk.imshealth.com>

>
> 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
no
> 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,

    select="'USA'=Billcode[2]/@country"

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():

    select="'USA'=normalize-space(Billcode[2]/@country)"

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:

    use='a[@corp="EvilEmpire"]'

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
perhaps

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.

Cheers,

Tom P


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]