Daemonite: Searching XML docs with XPATH Archive

Daemonite: Searching XML docs with XPATH Archive


Saturday, July 06, 2002
Searching XML docs with XPATH

CFMX ships with an XPATH engine that is called using the XMLSearch() function. It's great and absolutely dead simple to use. I've got a few code examples to share that I whipped up playing with http://fullasagoog.com/

XPATH is kind of like the SQL of XML. That's a pretty rude analogy but for developers who are use to extracting stuff from databases and are confronted with the prospect of getting info out of XML documents I think works.

FullAsAGoog.com is a website experiment that aggregates blog feeds together into a single feed. Pulling the excerpts from an RSS XML blog feed for display is very simple using XPATH in CFMX.

For example, lets play with a couple of random RSS 0.91-2 feeds (check the top of the XML document for the version).
http://www.flashguru.co.uk/FGMX101.xml
http://www.impossibilities.com/blog/rss.xml


  1. Grab these blog feeds and save them locally
  2. Use CFFILE to read one of the feeds into a variable like myFile
  3. Use the XMLParse() function to convert the variable into a CFMX XML Document (eg. <CFSET myXML = XMLParse(myFile)>)
  4. And if you thought that was easy, use XMLSearch() to extract the item elements as your list of news items like so <CFSET aResults = XMLSearch(myXML, "//item")>
  5. CFDUMP your aResults and see what you have

The only tricky bit is working out the right location path. In this short example the "//" means look for any element no matter how embedded in the XML document, that corresponds to "item". Blog feeds are a great place to start learning XML - they are very simple in structure and you could put a little news window on your site to boot.

To learn more about XPATH syntax you should have a look at the XML Path Language (XPath) Version 1.0 (http://www.w3.org/TR/xpath). Now normally reading these W3C recommendation should come with a government health warning but his one is really good. Scan through the doc and look for the great list of example "location paths". Then its just a case of hacking the code snippets for the path you need.

Posted by modius at 05:38 PM | Permalink
Trackback: http://blog.daemon.com.au/cgi-bin/dmblog/mt-tb.cgi/14

Comments

Found a half decent article on XPATH at developer.com, Understanding XPath:
http://softwaredev.earthweb.com/xml/article/0,,10697_1156211_1,00.html

Posted by: modius on July 8, 2002 08:36 PM

Ahah.. and now an EXCELLENT tutorial on XPATH:
http://www.zvon.org/xxl/XPathTutorial/General/examples.html

Posted by: modius on July 14, 2002 08:16 PM

If you want, you can supply any individual XML element object as the first argument of an XMLSearch() function call, but if you want to use the pass node as the current context, you must specify a relative path (e.g. "bar") NOT an absolute path like "/foo/bar" or the root node will be used, not the current context.

Posted by: Tim Blair on December 5, 2002 04:55 AM