I know what you’re thinking: “Chris, aren’t you supposed to be talking about CakePHP, or PHP, or your ever-changing working conditions?!?”. Alas, enough about my storied working career. Working here now means that I’m going to become Mr. “I know how to manipulate XML” in a hurry. Lucky for me, I can use PHP 5 and have access to SimpleXML to make fooling around with raw XML (is that actually a term?) so much easier. I remember working with PA Sportsticker XML feeds and having to write code to parse stuff out in PHP 4. Compare that to using SimpleXML in PHP 5. Go ahead, I’ll wait while you go and look it up.
So, for a project I’m working on (no names until it goes beta) I’m having to write and retrieve information from an XML document repository system (Resin + eXist). Now, I’m not too bad with database stuff but this system doesn’t use SQL. It uses something called XQuery to retrieve information. I can pull back XML snippets, whole XML documents, or just extract data from it. On the storing side, you can simply insert whole XML documents into it and then eXist takes care of indexing etc so you can look for information inside those documents.
Lucky for me, my company has a subscription to O’Reilly’s Safari system so I grabbed ‘XQuery Kick Start’ and started reading up on how xQuery works and how I can cheat and only learn what I needed to do for this first task. So, what I had to do was this:
Give me all games that belong to this league
So, I had the key for the league, and every document that was a game has the key for the league as an attribute. Now, if I was doing SQL you’d have something like:
SELECT * FROM games WHERE league_id = $leagueKey
and then you iterate through the results like normal. Let me tell you, xQuery is VERY different. Here’s what it looked like. My apologies for the link to the saved paste but it was looking terrible in preview.
Wow, not too intuitive at first glance, is it. Anyway, here’s what the output looked like:
2007-10-25 01:00:00AM|Monrovia Madness|Monrovia Madness,
2007-10-26 04:00:00AM|Monrovia Madness|Monrovia Madness,
Note the extra ‘,’ at the end. I’ll have to figure out how to stop it from doing that so I don’t have to do any code workarounds based on the output. Sorry for the ultra-boring and non-informative test data, but you gotta start somewhere. Now, the syntax does actually make a little bit of sense to me now that I’ve had the chance to muddle through from where I started. I’ll keep you posted as I get into some more-than-basic uses of xQuery.


Whoa… I thought I was looking at malformed XPath. I’d seen the term XQuery before and always thought it was an SQL to XPath mapper based loosely on the Java Content Repository’s theory of having a basic SQL grammar for talking with XML structures.
Seems like XPath be easier though: //games[@league_id = {$league_id}]
Is XQuery some sort of relation to XPath?
@Travis
I’m just going with what my predecessor gave me. eXist seems to use XQuery to pull stuff out. One of my co-workers has offered to do the heavy lifting wrt XQuery, so my job will be a little easier I guess.
Chris, in case you didn’t catch it, I thought I’d give you a heads up on a PHP5 extension for working with XQuery: http://www.zorba-xquery.com/index.php/24/http://www.zorba-xquery.com/index.php/24/