Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 19th, 2006, 06:04 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default Stumped: Contains() Method Woes

I'm trying to use the contains() method on this page, but I'm running into a strange problem. I've used this method on a similar page with success, so I'm not sure why I'm having this problem. Anyway, here's the code:
XML:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<formsdocs>

    <form id="1">
        <title>Form 1</title>
        <link>http://www.mysite.com/form1.aspx</link>
        <display>true</display>
    </form>

    <form id="2">
        <title>Form 2</title>
        <link>http://www.mysite.com/form2.aspx</link>
        <display>true</display>
    </form>

    <form id="3">
        <title>Form 3</title>
        <link>http://www.mysite.com/form3.aspx</link>
        <display>true</display>
    </form>

</formsdocs>
XSLT:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:variable name="keyword" select="''" />
    <xsl:template match="/">

        Keyword(s) entered: <xsl:value-of select="$keyword" />
        <br />
        <xsl:for-each select="formsdocs/formsdocs/form[contains(title, $keyword) and display = 'true']">
            <xsl:value-of select="" />
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
HTML RESULT:

Keyword entered: form
Result: No records show up.

Keyword entered: or
Result: Shows all records that contain "or", which is all 3 of them.

As you can see, no results show up even though the node "title" does contain "form". If I enter "or" instead, those records do show up. They don't show up if the word entered is at the beginning or the end of the "title" node value.

I've also tried using the normalize-space() method to trim any whitespace, even though there is none, but that had no affect. If anyone knows what could be the cause of this issue, I'd love to get some advice. I'm hoping that it's something simple;) Thanks.

KWilliams
 
Old January 19th, 2006, 06:14 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

contains is case sensitive.
You could use translate to turn the search term and title contents to both be lowercase.

As you say "or" works you seem to have mis-stated the XPath as there ard not two levels of formsdocs.

--

Joe (Microsoft MVP - XML)
 
Old January 19th, 2006, 06:19 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the truly rapid response.

Quote:
quote: contains is case sensitive.
I did not know that.

Quote:
quote:You could use translate to turn the search term and title contents to both be lowercase.
Could you give me an example of how to use the translate method? It would be very helpful.

Quote:
quote:As you say "or" works you seem to have mis-stated the XPath as there ard not two levels of formsdocs.
When I stated that "or" worked, I just meant that I had entered "or" (without quotes) into the search box to see what the result would be, not into the XPath expression itself. I just tried entering "Form" into the search box with success. (THANK YOU). So now all I need to do is do add the translate method to my form to get it working properly. Thanks so much for your help Joe:)

KWilliams
 
Old January 19th, 2006, 06:37 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, after a bit of research< I was able to come up with a solution using Joe's suggestion of the translate() method. This is how I did it:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:variable name="keyword" select="''" />
<xsl:variable name="lc">abcdefghijklmnopqrstuvwxyz</xsl:variable>
<xsl:variable name="uc">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
    <xsl:template match="/">

        Keyword(s) entered: <xsl:value-of select="$keyword" />
        <br />
        <xsl:for-each select="formsdocs/formsdocs/form[contains(translate(title, $uc, $lc), $keyword) and display = 'true']">
            <xsl:value-of select="" />
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

And it works great now. I'll make sure to use that method with any forms I have that use the contains() method. Thanks again Joe:)

KWilliams
 
Old January 19th, 2006, 06:40 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Code:
<xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/>

<xsl:variable name="lowerKeyword" select="translate($keyword, $upper, $lower)"/>

<xsl:for-each select="formsdocs/form[contains(translate(title, $upper, $lower), $lowerKeyword) and display = 'true']">
This approach is only going to work with traditional "English" characters but that might be good enough.

--

Joe (Microsoft MVP - XML)
 
Old January 19th, 2006, 06:44 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

For us it will be. We don't have any foreign characters in any of our data. For now, we plan on using Babalfish as a translation tool for users, so hopefully that will be enough. Thanks again.

KWilliams





Similar Threads
Thread Thread Starter Forum Replies Last Post
I'm stumped MadMutant BOOK: Beginning Access 2007 VBA ISBN: 978-0-470-04684-5 1 July 24th, 2008 05:29 PM
Stumped by Form pages... mmcdonal Access VBA 1 January 16th, 2008 01:58 PM
Completely Stumped Admiral1701 Classic ASP Professional 6 December 5th, 2006 02:35 PM
Stumped, need XSLT Help foobar XSLT 3 July 3rd, 2006 11:07 AM
This program has stumped me, help! :) c0mpgeeksportsfan JSP Basics 1 December 5th, 2004 01:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.