C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Doing case-InSensitve comparisons using XPath in XML


Posted Date: 29 Jun 2004    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: N.T.GopalakrishnanMember Level: Gold    
Rating: 1 out of 5Points: 7



Introduction

While XPath itself is a powerful feature, which we can use to traverse through XML Nodes anywhere around the xml document using simple syntax statements, we would often like to do those comparisons based on case-insensitiveness. But by default, xml is case-sensitive and so as the XPath search also.

The Translate function

The translate function is used to do a case-Insensitive comparisons for XPath queries. This function has 3 parameters - The string (or an Element or an Attribute) to compare, the set of individual characters to find in the string and then the set of individual replacement characters.

Seeing is beliveing - Code sample!!

Obviously, no explanation can go into the ears of techies like us, until we see something concrete (read code) given along with it. And I oblige. In the following sample, we just have a sample XML data island and we look at a small piece of code which does case-Insensitiveness comparisons on the loaded XML.

The XML...

Our sample XML looks like this:



<?xml version="1.0"?>
<BookStore>
<Books>
<book ISBN="ISBN- 1">
<Title>TITLE- 1</Title>
<Author>
<Name>Name 1</Name>
</Author>
<Price>10</Price>
</book>
<book ISBN="ISBN- 2">
<Title>TITLE- 2</Title>
<Author>
<Name>Name 2</Name>
</Author>
<Price>20</Price>
</book>
<book ISBN="ISBN- 3">
<Title>TITLE- 3</Title>
<Author>
<Name>Name 3</Name>
</Author>
<Price>30</Price>
</book>
<book ISBN="ISBN- 4">
<Title>TITLE- 4</Title>
<Author>
<Name>Name 4</Name>
</Author>
<Price>40</Price>
</book>
<book ISBN="ISBN- 5">
<Title>TITLE- 5</Title>
<Author>
<Name>Name 5</Name>
</Author>
<Price>50</Price>
</book>
<book ISBN="ISBN- 6">
<Title>TITLE- 6</Title>
<Author>
<Name>Name 6</Name>
</Author>
<Price>60</Price>
</book>
<book ISBN="ISBN- 7">
<Title>TITLE- 7</Title>
<Author>
<Name>Name 7</Name>
</Author>
<Price>70</Price>
</book>
<book ISBN="ISBN- 8">
<Title>TITLE- 8</Title>
<Author>
<Name>Name 8</Name>
</Author>
<Price>80</Price>
</book>
<book ISBN="ISBN- 9">
<Title>TITLE- 9</Title>
<Author>
<Name>Name 9</Name>
</Author>
<Price>90</Price>
</book>
<book ISBN="ISBN- 10">
<Title>TITLE- 10</Title>
<Author>
<Name>Name 10</Name>
</Author>
<Price>100</Price>
</book>
</Books>
</BookStore>



The Code..

As you can see from the above XML, only one element has an attribute and that element is "book". Also, it has only one attribute to it, which is ISBN. Now, what we are going to do is to do a simple case-insensitive comparison search for an element book, whose ISBN is say, "ISBN- 1" (remember, it can be in any case). First the code and then the explanation, OK?



Dim x As New XmlDocument
Dim s as String 'Remember to set the above XML to this string

x.LoadXML(s)

'The following 2 lines of code should be in one line

Dim s1 as string = "//*[translate(@ISBN,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"

s1 &= "=translate('isbn- 1','abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')]"

Dim xn As XmlNode = x.SelectSingleNode(s1)

If xn Is Nothing Then
MsgBox("Element not found!", MsgBoxStyle.Exclamation)
Else
MsgBox(xn.InnerXml)
End If



How translate works?

What we are trying to do In the above example is to make both LHS and RHS values to upper case and then do a comparison. The LHS is the actual attribute value in the xml and the RHS is the string that we are searching for. In our example, we are looking at an element called "book", whose ISBN attribute is "isbn- 1" (IN ANY CASE!!).



The translate works by doing character-by-character search in the source string and if found, it looks a corresponding replacement character at the same position at which the searching character was found in the 2nd parameter. What we are saying to the translate function in the second parameter is to look for all alphabets in the search string. Once it finds each of those in the search string, it first notes down at what position does this character exist in the second parameter. For example, the position of "a" is 1, "b" is 2,..., "i" is 9, "z" is 26. In our example, look at the second occurance of the translate function. It searches for a string called "isbn- 1". The translate function takes the list of characters given in the second parameter and searches whether any matching characters are there in the search string. When it first encouters the letter "i", it looks where the letter "i" is located in the second parameter. As we saw earlier, it is in position 9. Then now, it looks for a corresponding letter in the third parameter right at the same position. In our example, the third parameter has the letter "I" at the same position 9. So, now "i" is replaced with "I". And this goes on until the last character is searched.




What we are doing in our example is to convert both the actual attribute value and the comparing string to upper case and hence the node is returned to you, regardless of the case. In fact, the sample code checks in lower-case, while the actual xml has the values in upper-case.



Summary
Makes sense, friends??



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: New features in Visual Studio 2005 "Whidbey"
Previous Resource: Passing Multiple Parameters in DataGrid’s Asp:Hyperlink Column
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use