This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: string comparison
- From: "Andrew Welch" <awelch at piper-group dot com>
- To: <xsl-list at lists dot mulberrytech dot com>
- Date: Thu, 6 Jun 2002 12:33:45 +0100
- Subject: RE: [xsl] string comparison
- Reply-to: xsl-list at lists dot mulberrytech dot com
>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?
Sort of. You first need to convert everything to uppercase (or
lowercase), then test to see if its in there.
So,its usually best to set up the global variables (to save your
fingers):
<xsl:variable name="lowercase" select="'abcdefg... and so on'"/>
<xsl:variable name="uppercase" select="'ABCDEFG... and so on'"/>
You then need to convert both the search string and the text content to
uppercase (to negate case)
translate(name,$lowercase,$uppercase)
translate($searchString,$lowercase,$uppercase)
and then see if the searchString is in the name element
contains(name,$searchString)
So if you then put it all together, you might get something like this.
Here Ive used a param to accept the search string (defaulting to
'corneanu') and an extra variable holding it in uppercase.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:variable name="lowercase" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:param name="searchString" select="'corneanu'"/>
<xsl:variable name="upperSearchString"
select="translate($searchString,$lowercase,$uppercase)"/>
<xsl:template match="/">
<xsl:for-each
select="//contact[contains(translate(name,$lowercase,$uppercase),$upperS
earchString)]">
<xsl:apply-templates select="."/>
</xsl:for-each>
</xsl:template>
<xsl:template match="name">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
Honestly, it looks more way more complicated than it really is.
have fun!
cheers
andrew
-----Original Message-----
From: Corneanu Dan [mailto:cdan@savatech.ro]
Sent: 06 June 2002 11:42
To: xsl-list@lists.mulberrytech.com
Subject: [xsl] string comparison
Hi,
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?
Best regards, Dan.
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.350 / Virus Database: 196 - Release Date: 17/04/2002
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.350 / Virus Database: 196 - Release Date: 17/04/2002
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list