[racket] xml processing question

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Wed Dec 22 15:13:58 EST 2010

I usually use the SXML-related tools in PLaneT, rather than the "xexpr" 
ones.  They are a little tricky to get started with (I plan to improve 
this situation soon), so here is a simple demonstration using your 
example, to get you started:

---- Begin Example ----

#lang racket/base

(require (only-in racket/port call-with-input-string)
         (only-in (planet lizorkin/ssax:2:0/ssax) ssax:xml->sxml)
         (only-in (planet jim/sxml-match:1:1/sxml-match) sxml-match))

(define xmlin "
 <name>Some software</name>
 <more licence='gpl' checked='2010-12-10'/>
 <ref id='someref'/>
 <author id='John Doe'/>

(define sxml (call-with-input-string xmlin
                                     (lambda (in)
                                       (ssax:xml->sxml in '()))))

;; ==> (*TOP* (doc (name "Some software")
;;                 (more (@ (licence "gpl")
;;                          (checked "2010-12-10")))
;;                 (ref (@ (id "someref")))
;;                 (author (@ (id "John Doe")))))

(sxml-match (cadr sxml)
            ((doc (name ,name)
                  (more (@ (licence ,licence)
                           (checked ,checked)))
                  (ref (@ (id ,ref)))
                  (author (@ (id ,author))))
             (format "The author is ~A." author)))
;; ==> "The author is John Doe."

---- End Example ----

Note that, in practice, you will mix these SXML tools with low-level 
list-processing tools.  For a simple example, in the example above I 
used "cadr" as a convenient way to get the first (and only) top-level 
SXML element.


Posted on the users mailing list.