Using XPath functions within XMLDOM...???
Posted on 2006-10-30
I'm currently using XML datasets to display product information to my pages. I'm using SelectNodes to grab all nodes that I want, and then I loop through the selected nodes to display each product to the page. So, when I'm browsing by categories, for instance, I have something like this:
Set ProductsList = xmlDoc.SelectNodes("/Inventory/Product[Category='Windows Servers']")
This gives me a node set of all Windows Server products in our inventory and I loop through the set to display each to the page. Works great.
What I'm trying to do now is make this searchable. So instead of selecting by category I'll select by taking submitted keywords, matching them up with the descrpitions of the products in the XML document, and then creating the node set based on matching products. So at first I simply change the line to:
Set ProductsList = xmlDoc.SelectNodes("/Inventory/Product[Description='" & SearchQuery & "']")
Of course, this only works if SearchQuery is exactly the same as one of the Product/Description elements. In SQL this would basically be saying
SELECT * FROM Products WHERE Description = 'whatever_was_searched'
The SQL way of switching this into a dynamic search would be as simple as this:
SELECT * FROM Products WHERE Description LIKE '%whatever_was_searched%'
I looked up XPath operators and all I find are:
= (equal to)
!= (not equal to)
< (less than)
> (greater than)
<= (less than or equal to)
>= (greater than or equal to)
So none of those will work. I dig a little further and I find there's a function used in XPath called contains and that is what I should use. However, I'm having trouble with the syntax of this. I found a guide at microsoft.com that says to use it like this:
Set nlist = doc.selectNodes("//book/Title[contains(.,'ADO')]")
that particular example would return all books where the word ADO exists in the Title. So I try and implement this to my own use:
Set ProductsList = xmlDoc.SelectNodes("/Inventory/Product/Description[contains(., 'Windows']")
But I'm getting an unknown method error.
Any information on how to make this work would be greatly appreciated. Thanks!