Reading XML Files in WSH - Constructing advanced queries
(Page 5 of 6 )
I bet right about now you’re wondering whether or not you can combine all of these techniques to create even more complex queries. In short, the answer is: yes. What fun would it be if you couldn’t do that?!
Set colTitles = objXmlDoc.selectNodes( _
"/playlist/trackList/track[creator = 'Breaking Benjamin']/title")
For Each objTitle In colTitles
WScript.Echo objTitle.Text
Next
This query combines the two techniques we’ve seen already by first filtering results based upon the creator element, and then only returning the specified title element.
Wait, I know your next question. And yes, you are exactly right. You can specify more than one specific field even when using a complex query like this.
Set colProperties = objXmlDoc.selectNodes( _
"/playlist/trackList/track [creator = 'Breaking Benjamin']" _
& "/(title | album)")
For Each objProperty In colProperties
WScript.Echo objProperty.Text
Next
This query first returns all elements that match the path <playlist><trackList><track>. Then it filters those results and returns only those whose creator is “Breaking Benjamin.” Finally, it returns only the title and album elements for those tracks.
Take a breath. It looks a whole lot harder than it really is. Look at the query one piece at a time and think about how it filters the results. Each piece builds upon the results from the piece before. It’s a pretty logical process. You’ll get a handle on it in no time.
It’s okay if you need to read through this section again. These queries are a little unusual to look at when you first begin. The best, and easiest, way to learn them is to grab an XML file and just start playing. I promise, if you try them out and watch the results, you’ll pick this up in no time at all.
Next: What to do when it doesn’t work >>
More Windows Scripting Articles
More By Nilpo