; XML processing in Scheme ; SXML to XML/HTML conversions and ; SXSLT, XML Stylesheet Language Transformations in Scheme ; $ Id: SXmanip.scm,v 1.5 2001/02/27 23:43:39 oleg Exp oleg $ (include "myenv.scm") (include "SXML-tree-trans.scm") ; Running examples: SXML data structures (define tree1 '(html (head (title "Slides")) (body (p (@ (align "center")) (table (@ (style "font-size: x-large")) (tr (td (@ (align "right")) "Talks ") (td (@ (align "center")) " = ") (td " slides + transition")) (tr (td) (td (@ (align "center")) " = ") (td " data + control")) (tr (td) (td (@ (align "center")) " = ") (td " programs")))) (ul (li (a (@ (href "slides/slide0001.gif")) "Introduction")) (li (a (@ (href "slides/slide0010.gif")) "Summary"))) ))) (define tree2 '(Forecasts (@ (TStamp "958082142")) (TAF (@ (TStamp "958066200") (LatLon "36.583, -121.850") (BId "724915") (SName "KMRY, MONTEREY PENINSULA")) (VALID (@ (TRange "958068000, 958154400")) "111730Z 111818") (PERIOD (@ (TRange "958068000, 958078800")) (PREVAILING "31010KT P6SM FEW030")) (PERIOD (@ (TRange "958078800, 958104000") (Title "FM2100")) (PREVAILING "29016KT P6SM FEW040")) (PERIOD (@ (TRange "958104000, 958154400") (Title "FM0400")) (PREVAILING "29010KT P6SM SCT200") (VAR (@ (Title "BECMG 0708") (TRange "958114800, 958118400")) "VRB05KT")) ))) ; Converting SXML to XML/HTML by direct evaluation of SXML, ; as if it were a directly executable code (define (entag tag) (lambda elems (if (and (pair? elems) (pair? (car elems)) (eq? '@ (caar elems))) (list #\< tag (cdar elems) #\> (cdr elems) " #\newline) (list #\< tag #\> elems " #\newline)))) (define (enattr attr-key) (lambda (value) (if (eq? value #t) (list #\space attr-key) (list #\space attr-key "=\"" value #\")))) (define html (entag "HTML")) (define head (entag "HEAD")) (define title (entag "TITLE")) (define body (entag "BODY")) (define p (entag "P")) (define table (entag "TABLE")) (define tr (entag "TR")) (define td (entag "TD")) (define ul (entag "UL")) (define li (entag "LI")) (define a (entag "A")) (define (@ . elems) (cons '@ elems)) (define align (enattr "ALIGN")) (define style (enattr "STYLE")) (define href (enattr "HREF")) ; It's instructive to do the following: ;(pp (eval tree1)) (SRV:send-reply (eval tree1)) #| (pp `(ul ,@(map (lambda (filename-title) `(li (a (@ (href ,(car filename-title)))) ,(cdr filename-title))) '(("slides/slide0001.gif" . "Introduction") ("slides/slide0010.gif" . "Summary"))) )) |# ;------------------------------------------------------------------------ ; Tree traversals ; tree is understood to be a grove as well: ; ::= | ; where (see SXPath.scm) ; ::= (name ...) | "text string" ; An (ordered) set of nodes is just a list of the constituent nodes: ; ::= ( ...) ; where 'name' is a symbol. ; The following three procedures are borrowed from SXPath.scm (define (nodeset? x) (or (and (pair? x) (not (symbol? (car x)))) (null? x))) ; Apply proc to each element of lst and return the list of results. ; if proc returns a nodeset, splice it into the result (define (map-union proc lst) (if (null? lst) lst (let ((proc-res (proc (car lst)))) ((if (nodeset? proc-res) append cons) proc-res (map-union proc (cdr lst)))))) ; The criterion 'crit' is a symbol, one of the following: ; id - tests if the Node has the right name (id) ; @ - tests if the Node is an ; * - tests if the Node is an ; *text* - tests if the Node is a text node ; *PI* - tests if the Node is a PI node ; *any* - #t for any type of Node (define (node-typeof? crit) (lambda (node) (case crit ((*) (and (pair? node) (not (memq (car node) '(@ *PI*))))) ((*any*) #t) ((*text*) (string? node)) (else (and (pair? node) (eq? crit (car node)))) ))) ; The following procedure is the most generic transformation of SXML ; into the corresponding XML document. The SXML tree is traversed ; post-oder (depth-first) and transformed into another tree, which, ; written in a depth-first fashion, results in an XML document. ; Unlike the '(eval tree1)' example above, there is no need to declare ; a specific binding for each tag beforehand. The computation below is ; however very similar to call-by-value. Yet there is a twist: an ; extension of bindings before argument expressions are evaluated. ; The procedure below processes all elements and attributes in an SXML ; tree uniformly. The example however can easily be amended to handle ; a certain element in a particular way, or to omit it from the ; resulting XML document. (SRV:send-reply (post-order tree1 `((@ ((*default* ; local override for attributes . ,(lambda (attr-key . value) ((enattr attr-key) value)))) . ,(lambda (trigger . value) (list '@ value))) (*default* . ,(lambda (tag . elems) (apply (entag tag) elems))) (*text* . ,(lambda (trigger str) str)) )) ) ; Give the list of all href attributes in tree1 (pp (post-order tree1 `((href ((*text* . ,(lambda (trigger str) str))) . ,(lambda (trigger value) value)) (*text* . , (lambda (trigger value) '())) (*default* . ,(lambda (trigger . value) value)))) ) (pp (post-order tree1 `((href ((*text* . ,(lambda (trigger str) str))) . ,(lambda (trigger . value) value)) (*text* . , (lambda (trigger value) '())) (*default* . ,(lambda (trigger . values) (apply append values))))) ) ; Pre-order traversal of a tree and creation of a new tree: ; apply-templates:: tree x -> ; where ; ::= (