Tuesday, November 21, 2006

More XPath and XML in Lisp

I've started playing with XML again in lisp, this time to support a timeline wiki. Eventually with semantics. This idea will take a little while to fully germinate. In the meantime, I've been extending my mini-XPath implementation. I was able to implement an expansion to pick out the children of a node with a particular filler for the class attribute:

(child * [ (attribute "class") = "preceding-event" ] )

Yeah, it's still pretty simple, and it doesn't start with the compact syntax, but you have to start somewhere, right?

I also implemented a set of macros for manipulating an XML document by replicating a section of the document as a template:

(with-xml-template (:node xml)
(do-events (e)
(with-xml-template (:class "preceding-event")
(dolist (pe (event-preceding-events e))
(fill-template (event-name pe))))
(with-xml (:class "event")
(with-xml (:class "event-start-time")
(fill-template (event-start-time e)))
(with-xml (:class "event-name")
(fill-template (event-name e)))
(with-xml (:class "event-end-time")
(fill-template (event-end-time e))))
(with-xml-template (:class "succeeding-event")
(dolist (se (event-succeeding-events e))
(fill-template (event-name se))))))

And some sample XML from this little hack:

<div class="events-list">

<div class="event-data">

<p class="event">
<span class="event-start-time">3373112514</span>
<span class="event-name">event2</span>
<span class="event-end-time">3373114514</span>
<p class="succeeding-event">event1</p>

</div><div class="event-data">
<p class="preceding-event">event2</p>
<p class="event">
<span class="event-start-time">3373114514</span>
<span class="event-name">event1</span>
<span class="event-end-time">3373119514</span>

<p class="succeeding-event">event4</p>

Again, not particularly fancy, but this simple hack gives me a lot of flexibility in how I assemble my XML document, without having to worry about how changes will impact my lisp code. Lisp is good for hacking structures, yet I don't find little tools like these all that easily. There isn't yet a standard DOM API defined for Lisp, the way there is for Java. I don't see what's keeping us from this. CXML has a full set of objects and generic functions corresponding to the DOM2 API. Why isn't this API available for reuse, independent of the parser?