RE: [xsl] Changing case using translate()

Subject: RE: [xsl] Changing case using translate()
From: "Andrew Welch" <andrew@xxxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 15 Feb 2001 14:29:45 -0000
Thanks, that works perfectly - however I have one further, slightly
un-related issue:

The code

TBD/business[contains(translate(*[local-name() = $param1],$lowercase,
$uppercase),translate($param2, $lowercase, $uppercase)]

works fine if there is only one child element of business, such as 'name'.
However, each business node currently contains several <keyword> child
nodes, that don't get picked up by the above.  If I reduce the number of
<keyword> nodes to one (for each business node) then it searches no problem.


Searching for 'A' on the above xml produces no results.


However, searching for 'A' here returns the required result because there is
only a single <keyword> element.

Is this a problem with the structure of my xml files, or is there a way of
modifying the search line to take into account multiple elements with the
same name???

Thanks once again for all the help



Hi Andrew,

> As I understand it, case conversion is done using
> translate(xxxx,abcdefghijklmnopqrstuvwxyz,ABCDEFGHIJKLMNOPQRSTUVWXYZ)

Well, you have to have quotes around the alphabets so that they're
recognised as strings, but basically you're right.

To save time, I'd save the alphabets in variables:

<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<xsl:variable name="lowercase" select="'abcdefghijklmnopqrstuvwxyz'" />

> Im trying to incorporate it into the following line of code, to allow
> case-independant searching:
> <xsl:for-each select="TBD/business[contains(*[local-name() = $param1],
> By converting both params to uppercase it should allow, for example,
> 'rising' to match 'Rising Sun'.

They should go around both of the values that you're comparing. You
don't want to translate $param1 because that will change which child
of the business element you're testing.  Rather, you want to

  *[local-name() = $param1]

(i.e. the value of the business element's child called $param1) with:

  translate(*[local-name() = $param1], $lowercase, $uppercase)

The second value is $param2, so changing that to uppercase can be done

  translate($param2, $lowercase, $uppercase)

Putting this together, you get the select expression:

  TBD/business[contains(translate(*[local-name() = $param1],
                                  $lowercase, $uppercase),
                        translate($param2, $lowercase, $uppercase))]

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread