XML Parsing with FileMaker and bBox

In order to more easily work with XML data, I’ve added the libxml2 library to version 0.54 of the bBox plug-in.

Among other things, libxml2 provides support for XPath 1.0 queries, which can be used to easily extract or set values from XML data, so XPath was my first choice. Although XML has lost a bit of its shine as JSON has come into vogue, XML is still integral to many systems, including FileMaker. Also, the XPath routines can be used for similar syntaxes, so for instance, you might find it helpful for scraping data from a HTML page.

The first two functions aren’t very interesting, but they are essential. XPathInitialize is used to initialize the XPath environment (surprise!) and do the initial parsing of the XML source. XPathFinalize releases the memory used for the tokenized XML and the XPath environment. XPathEvaluate is where most of the fun happens, as this is where we pass in our XPath statements and get back our results.

Getting Started

Even with a few script steps and some simple XPath statements we can do some interesting stuff. To start off, we’ll need some XML to experiment with. I’ll use a modified version of the example XML from wikiboooks.org:

A list of books useful for people first learning how to build web XML web applications.

XQuery
Priscilla Walmsley
Este libro es un recorrido muy detallado, a través de y completo del Lenguaje de Consulta del W3C. Cubre todos los aspectos clave de la lengua.
Trade press
Commercial
49.95

XQuery Examples
Chris Wallace
Dan McCreary
This book provides a variety of XQuery example programs and is designed to work with the eXist open-source native XML application server.
Wiki-books
Creative Commons Sharealike 3.0 Attribution-Non-commercial
29.95

XForms Tutorial and Cookbook
Dan McCreary
This book is an excellent guide for anyone that is just beginning to learn the XForms standard. The book is focused on providing the reader with simple, but complete examples of how to create XForms web applications.
Wikibook
Creative Commons Sharealike 3.0 Attribution-Non-commercial
29.95

XRX: XForms, Rest and XQuery
Dan McCreary
This book is an overview of the key architectural and design patterns.
Wikibook
Creative Commons Sharealike 3.0 Attribution-Non-commercial
29.95

Setup

We can consider the XML above as our database that we will be running our queries against. The FileMaker script steps required to do a basic query are pretty simple. Here’s a snippet that does most of what you might need:

Set Variable [ $result; bBox_XPathInitializeFromText (0; DEMO::xml_source)
#
If [ $result = “” ]
Set Variable [ $result; bBox_XPathEvaluate(DEMO::xpath_expression; DEMO::xpath_node_delimiter)
End If
#
Set Variable [ $ignore; Value:bBox_XPathFinalize ] #

XPath Examples

The XPath statements, in their simplest forms, look identical to unix style file paths. These are what you’d be passing as the path_expression above. Our first example just returns all titles from the above XML:

/library/book/title

A slightly more complex form that returns all titles and their retail prices would be (“//” returns the following node from anywhere in the hierarchy):

//title | //list-price

And here we use one of the XPath functions to get the last book in our list:

/library/book[last()]

Conclusion

XML does have its arcane side, so some capabilities will require some effort to learn, and may be beyond what the current implementation in bBox can handle. Also, XPath functions are fairly strict in their interpretation of XML, so any poorly formed XML is likely to be rejected by the bBox_XPathInitialize call. Use the Console utility or go directly to your system log at /var/log/system.log to check for more detailed information. The xmllint command may also be helpful here.

There is quite a bit more that can be done with XPath, so don’t stop looking here for examples. You can also pair up the XPath functions with the bBox’s curl function to pull in remote content.

Simon

Leave a Reply