Tuesday, August 15, 2006

XPath - bigger and better?

I recently started looking at XPath again, as a convenient way of manipulating XML documents programmatically. Natually, I needed a lisp implementation on top of the framework that cxml provides. And not finding any obvious candidates, I started rolling my own, starting with something really simple, with a lispy syntax. And to come up with the lispy syntax, I stared to refer to the XPath documents at w3.org. And that's when the fun began. My, how XPath has grown! Don't get me wrong, some of the things in there seem to be fine ideas, like unifying XPath with XQuery and coming up with some formal semantics. But the reference material right now is unreadable.

So I'm starting with a syntax inspired by XPath 1.0, hoping that the semantics haven't changed drastically since then:

(expand-path '(child "xhtml:div" / child "xhtml:p")) ->
((LOOP FOR NODE IN (DOM:CHILD-NODES CONTEXT) WHEN
(AND (DOM:ELEMENT-P NODE)
(PURI:URI= (DOM:NAMESPACE-URI NODE)
#<URI http://www.w3.org/1999/xhtml>)
(STRING= (DOM:LOCAL-NAME NODE) "div"))
COLLECT NODE)
(LOOP FOR NODE IN (DOM:CHILD-NODES CONTEXT) WHEN
(AND (DOM:ELEMENT-P NODE)
(PURI:URI= (DOM:NAMESPACE-URI NODE)
#<URI http://www.w3.org/1999/xhtml>)
(STRING= (DOM:LOCAL-NAME NODE) "p"))
COLLECT NODE))


Naturally, some more work is still needed to actually apply these fragments to an XML document.