XPath- multiple criteria in a contains() expression
There are times when working on technical projects where you hit a roadblock. There is some technical glitch, and you begin doing newsgroup searches using every keyword permutation you can think of. You know you can do some kind of work-around, but you refuse to take that road. You may stay on the issue for hours, perhaps even days- against all of your better judgment and any common sense.
I just had one of those. I wanted to use the contains keywork in an XPath expression, but rather than just search on the text or attribute text for an element using a single search term, I wanted to search for multiple terms. For example, here's some XML:
<comment>
<body>I am very happy with my Trek Madone 5.9. Thanks for producing such a fine bicycle.</body>
</comment>
<comment>
<body>I test rode a Colnago and found it to be an excellent ride. But, it was not superior to the 5.9 I purchased.</body>
</comment>
Now, I wanted to do a search where I look for 'Trek' or 'Colnago' using contains() . Well, there was no example of doing exactly what I wanted to do. Most examples ust show a single search term in a simple contains expression. That is easy enough for the element or for a simple attribute. However, what if I want to search the element or attribute for multiple terms? Trial and error paid off, and I came up with this which works perfectly. I would like to know what the perf stats would be like on a huge file though:
/comments/comment[contains(body,'Madone') or contains(body, 'Trek') or contains(body, 'Colnago')]/.
I am not sure if this is really how I am supposed to do it, but IT WORKED! It is satisfying sometimes just to make it past the roadblock.
This will figure into my demo for Office XML at Tech Ed this year.
Rock Thought for the Day: Q magazine is a pretty good mag for rock and roll devotees. They also do very well researched and well written special issues. Check out their history of rock and roll issues here: https://www.q4music.com/nav?page=q4music.page&fixture_page=116524&resource=116524. I have loved reading these.
Rock On
Comments
- Anonymous
May 02, 2005
It's probably not fast, but it looks good to me. :)
XPath reference:
http://www.w3.org/TR/xpath#section-String-Functions - Anonymous
May 02, 2005
Your solution only partially works. The XPath contains function will succeed even when the string you are looking for is part of a word. This could lead to false hits, if you were assuming that you were looking for "words" in the body element rather than "substrings".