We help IT Professionals succeed at work.

Case Insensitive search using XPath

anagm
anagm asked
on
Medium Priority
359 Views
Last Modified: 2012-05-05
I am trying to make the search case insensitive

existing snippet:
if (frmMain.txtName.value)
{
query += "@Name='" + frmMain.txtName.value + "'";
}

I modified the code as below


if (frmMain.txtName.value)
{
query += "translate(@Name,'ABC','abc')='" + frmMain.txtName.value.toLower() + "'";
}

it does not seem to work.  

what i am missing in the above code

any help would be appreciated.
anagm
Comment
Watch Question

Author

Commented:
I tried like this:

if (frmMain.txtName.value)
{
query += ["translate(@Name,'ABC','abc')='" + frmMain.txtName.value + "'"];
}

i get an error:
msxml3.dll: Unknown method.

//Property[-->translate(@<--Name,'ABC','abc')='liberty' and so on ....
CERTIFIED EXPERT

Commented:
try

query +="frmMain.txtName.value.toLower() = translate(@Name,'ABC','abc')";

or

query += [Name = translate(frmMain.txtName.value.toLower() , 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')]";

CERTIFIED EXPERT

Commented:
Weiping DuSenior Web Developer
CERTIFIED EXPERT

Commented:
What if you have upper case 'D', 'E'... ?    
Does translate(@Name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') work?

Author

Commented:
Right now if i give as string for example 'NEBRASKA' . It gives me a single record from the database.  But if i give 'nebraska' then it does not give me any record.

I want that to be case insensitive like for example
whether i give 'NEBRASKA'  or 'nebraska'.  

I should get a record from the database.

so,  owenli27

Should i try with all the alphabets in the translate function?

translate(@Name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')



YZlat,  i am still trying your solution






Weiping DuSenior Web Developer
CERTIFIED EXPERT

Commented:
is that JavaScript or VB?
It should be toLower() Or toLowerCase()

Author

Commented:
owenli27 , it is javascript

Author

Commented:
YZlat,  the 'Name' field is associated with some other business logic.

Now i am trying for 'Code' field, which is txtCode

for the below code
if (frmMain.txtCode.value)
{
if (code.substr(0, frmMain.txtCode.value.length) == frmMain.txtCode.value)
{
//logic
}

I am giving the Code, which is txtCode as 'YZLAT' and i get a record

but if i give 'yzlat' it gives me a message saying, 'no records are found'

i want to make this case insensitive,

YZlat, i need some more help please

anagm
Weiping DuSenior Web Developer
CERTIFIED EXPERT

Commented:
Your database may store data with all upper case.  If so, you need to do translation to upper case, something like:
query += "translate(@Name,'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')='" + frmMain.txtName.value.toUpperCase() + "'";

Author

Commented:
yes, you are right.  in the database the 'name' is in the upper case

so i will be using as follows for 'Name'

query += "translate(@Name,'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')='" + frmMain.txtName.value.toUpper() + "'";

Can i get some help for the 'Code' field

below is the code:

if (frmMain.txtCode.value)
{
if (code.substr(0, frmMain.txtCode.value.length) == frmMain.txtCode.value)
{
//logic
}


how can i make this case insensitive.  
the 'Code's are in the upper case in the database too

any help would be greatly appreciated
anagm

Weiping DuSenior Web Developer
CERTIFIED EXPERT

Commented:
Try this to see if it work.
if (code.toUpperCase().substr(0, frmMain.txtCode.value.length) == frmMain.txtCode.value.toUpperCase())

Author

Commented:
yup, it works for the 'txtCode'


I tried with the below code for the 'txtName'

query += "translate(@Name,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')='" + frmMain.txtName.value.toUpperCase() +"'";

and i get this error  'msxml3.dll: Unknown method.'

the syntax seems to be correct, what modification i should do?

please help

Author

Commented:
This is for the 'txtName'.  


In the database the field value is stored as, for example 'United States'.

That is the first letter is the capital letter.

when i do the search by 'United States', i get a record.
But if i do a search with a keyword 'united states', i DO NOT get a record.

i am trying with the below code:

query += "translate(@Name,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')='" + frmMain.txtName.value.toUpperCase() +"'";

i get an error ''msxml3.dll: Unknown method'


Weiping DuSenior Web Developer
CERTIFIED EXPERT

Commented:
Did you run xmlinst.exe after you installed msxsml3.dll?  And what is namespace in your xslt?
Weiping DuSenior Web Developer
CERTIFIED EXPERT

Commented:
Is "@Name" an attribute which is in a xml element Or it just is a constant string which is used in SQL to query database?
Can you post more snppet code?  I wonder if we can assign "@Name" to a Javascript variable if it stands for an attribute name.

Author

Commented:
owenli27,

this is how i am getting the error for the below code:

if (frmMain.txtName.value)
{
query += "translate(@Name,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')='" + frmMain.txtName.value.toUpperCase() +"'";
}

var xpathQuery = "//Property"
if (query)
{
xpathQuery += "[" + query + "]";
}

get the following error:                        

msxml3.dll: Unknown method.

//Property[-->translate(@<--abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')='UNITED STATES' and @Language='ENGLISH']

how do i run xmlinst.exe after you installed msxsml3.dll?

need some more help please

Author

Commented:
owenli27,

It is the name of the column in the table,

that is i have column with name 'Name' in the table

Weiping DuSenior Web Developer
CERTIFIED EXPERT

Commented:
I just made a test by using

<xsl:if test="customerName=//customer[translate(@member,'yesxz','YESXZ') = 'YES']/customerName">
    <xsl:value-of select ="." />
</xsl:if>

Against my xml like:

<customer member="yes">
    <customerName>Mary</customerName>
    <gender>Female</gender>
</customer>

The test works fine when I use ActiveXObject("Microsoft.XMLDOM").  But it doesn't work if I use ActiveXObject("MSXML2.DOMDocument").
Weird, the different processor cause problems.  So, I guess that we may have to give up translate() and try other way.
Senior Web Developer
CERTIFIED EXPERT
Commented:
Your database store 'Name' with frist character being uppercase. I create a javascript function to convert txtName input to match database name column format.

function convert(txtStr)
{
      var tmpName = txtStr.toLowerCase();
      var result  = "";
      var flag = 0;
      for (var i = 0; i < tmpName.length; i++)
      {
            if(i == 0){
                result += tmpName.charAt(i).toUpperCase()
                continue;
            }
             if (tmpName.charAt(i) == " "){
                  result += tmpName.charAt(i)
                   flag = 1;
                  continue;
            }
            if(flag == 1){
                result += tmpName.charAt(i).toUpperCase();
                flag = 0;
            }
            else
                result += tmpName.charAt(i);
       }
       return result;
}

if (frmMain.txtName.value)
{
      var temp = convert(frmMain.txtName.value);
      query += "@Name='" + temp + "'";
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
yup, it worked.  Thank you so much owenli27

YZlat,  I am able to get this done by owenli27 solution.  I am going to give full points to him/her.

Thank you for helping me, YZLat.



 

Author

Commented:
I need some more help from both of you,

I am posting another questions with Subject:  "Searching the database with the given text" in the textbox field

thank you

Author

Commented:
sorry, its going to be

"Searching the table with the given text"
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.