Re: [xsl] string comparison

Subject: Re: [xsl] string comparison
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 6 Jun 2002 12:13:25 +0100
Hi Dan,

>  I have a document made up from a collection of addresses like this:
> <address-book> 
>  <contact> 
>         <name>Dan Corneanu Cornel</name>  
>        .....
>  </contact> 
>  <contact> 
>          <name>Florin Corneanu</name> 
>          ...... 
>  </contact> 
>     ........ 
> </address-book> 
>
> How can I select all the contacts which contain the string 
> "corneanu" or "Corneanu" or "cOrneanu" etc. in the <name> 
> child? Is there something like the 'like()' function from SQL?

You can select all the contacts whose name contains 'Corneanu' with:

  /address-book/contact[contains(name, 'Corneanu')]

As you no doubt know, though, contains() is a case-sensitive function.
You make it case-insensitive by converting the name (and the string
that you're testing with) to lowercase, which you can do with the
translate() function, as follows:

  /address-book/contact
    [contains(translate(name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
                              'abcdefghijklmnopqrstuvwxyz'), 'corneanu')]

If you only test for 'Corneanu', you may as well just convert those
letters:

  /address-book/contact
    [contains(translate(name, 'CORNEAU',
                              'corneau'), 'corneanu')]

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


Current Thread