| 1 |
; XML processing in Scheme |
| 2 |
; SXML to XML/HTML conversions and |
| 3 |
; SXSLT, XML Stylesheet Language Transformations in Scheme |
| 4 |
|
| 5 |
; $ Id: SXmanip.scm,v 1.5 2001/02/27 23:43:39 oleg Exp oleg $ |
| 6 |
|
| 7 |
(include "myenv.scm") |
| 8 |
(include "SXML-tree-trans.scm") |
| 9 |
|
| 10 |
; Running examples: SXML data structures |
| 11 |
|
| 12 |
(define tree1 |
| 13 |
'(html |
| 14 |
(head (title "Slides")) |
| 15 |
(body |
| 16 |
(p (@ (align "center")) |
| 17 |
(table (@ (style "font-size: x-large")) |
| 18 |
(tr |
| 19 |
(td (@ (align "right")) "Talks ") |
| 20 |
(td (@ (align "center")) " = ") |
| 21 |
(td " slides + transition")) |
| 22 |
(tr (td) |
| 23 |
(td (@ (align "center")) " = ") |
| 24 |
(td " data + control")) |
| 25 |
(tr (td) |
| 26 |
(td (@ (align "center")) " = ") |
| 27 |
(td " programs")))) |
| 28 |
(ul |
| 29 |
(li (a (@ (href "slides/slide0001.gif")) "Introduction")) |
| 30 |
(li (a (@ (href "slides/slide0010.gif")) "Summary"))) |
| 31 |
))) |
| 32 |
|
| 33 |
|
| 34 |
(define tree2 |
| 35 |
'(Forecasts (@ (TStamp "958082142")) |
| 36 |
(TAF (@ (TStamp "958066200") (LatLon "36.583, -121.850") |
| 37 |
(BId "724915") (SName "KMRY, MONTEREY PENINSULA")) |
| 38 |
(VALID (@ (TRange "958068000, 958154400")) "111730Z 111818") |
| 39 |
(PERIOD (@ (TRange "958068000, 958078800")) |
| 40 |
(PREVAILING "31010KT P6SM FEW030")) |
| 41 |
(PERIOD (@ (TRange "958078800, 958104000") (Title "FM2100")) |
| 42 |
(PREVAILING "29016KT P6SM FEW040")) |
| 43 |
(PERIOD (@ (TRange "958104000, 958154400") (Title "FM0400")) |
| 44 |
(PREVAILING "29010KT P6SM SCT200") |
| 45 |
(VAR (@ (Title "BECMG 0708") (TRange "958114800, 958118400")) |
| 46 |
"VRB05KT")) |
| 47 |
))) |
| 48 |
|
| 49 |
; Converting SXML to XML/HTML by direct evaluation of SXML, |
| 50 |
; as if it were a directly executable code |
| 51 |
|
| 52 |
(define (entag tag) |
| 53 |
(lambda elems |
| 54 |
(if (and (pair? elems) (pair? (car elems)) (eq? '@ (caar elems))) |
| 55 |
(list #\< tag (cdar elems) #\> (cdr elems) "</" tag #\> #\newline) |
| 56 |
(list #\< tag #\> elems "</" tag #\> #\newline)))) |
| 57 |
(define (enattr attr-key) |
| 58 |
(lambda (value) |
| 59 |
(if (eq? value #t) (list #\space attr-key) |
| 60 |
(list #\space attr-key "=\"" value #\")))) |
| 61 |
|
| 62 |
(define html (entag "HTML")) |
| 63 |
(define head (entag "HEAD")) |
| 64 |
(define title (entag "TITLE")) |
| 65 |
(define body (entag "BODY")) |
| 66 |
(define p (entag "P")) |
| 67 |
(define table (entag "TABLE")) |
| 68 |
(define tr (entag "TR")) |
| 69 |
(define td (entag "TD")) |
| 70 |
(define ul (entag "UL")) |
| 71 |
(define li (entag "LI")) |
| 72 |
(define a (entag "A")) |
| 73 |
|
| 74 |
|
| 75 |
(define (@ . elems) (cons '@ elems)) |
| 76 |
(define align (enattr "ALIGN")) |
| 77 |
(define style (enattr "STYLE")) |
| 78 |
(define href (enattr "HREF")) |
| 79 |
|
| 80 |
|
| 81 |
; It's instructive to do the following: |
| 82 |
;(pp (eval tree1)) |
| 83 |
|
| 84 |
(SRV:send-reply (eval tree1)) |
| 85 |
|
| 86 |
#| |
| 87 |
(pp `(ul |
| 88 |
,@(map (lambda (filename-title) |
| 89 |
`(li (a (@ (href ,(car filename-title)))) |
| 90 |
,(cdr filename-title))) |
| 91 |
'(("slides/slide0001.gif" . "Introduction") |
| 92 |
("slides/slide0010.gif" . "Summary"))) |
| 93 |
)) |
| 94 |
|# |
| 95 |
|
| 96 |
;------------------------------------------------------------------------ |
| 97 |
; Tree traversals |
| 98 |
; tree is understood to be a grove as well: |
| 99 |
; <tree> ::= <Node> | <Nodeset> |
| 100 |
; where (see SXPath.scm) |
| 101 |
; <Node> ::= (name <Node> ...) | "text string" |
| 102 |
; An (ordered) set of nodes is just a list of the constituent nodes: |
| 103 |
; <Nodeset> ::= (<Node> ...) |
| 104 |
; where 'name' is a symbol. |
| 105 |
|
| 106 |
; The following three procedures are borrowed from SXPath.scm |
| 107 |
(define (nodeset? x) |
| 108 |
(or (and (pair? x) (not (symbol? (car x)))) (null? x))) |
| 109 |
|
| 110 |
; Apply proc to each element of lst and return the list of results. |
| 111 |
; if proc returns a nodeset, splice it into the result |
| 112 |
(define (map-union proc lst) |
| 113 |
(if (null? lst) lst |
| 114 |
(let ((proc-res (proc (car lst)))) |
| 115 |
((if (nodeset? proc-res) append cons) |
| 116 |
proc-res (map-union proc (cdr lst)))))) |
| 117 |
|
| 118 |
; The criterion 'crit' is a symbol, one of the following: |
| 119 |
; id - tests if the Node has the right name (id) |
| 120 |
; @ - tests if the Node is an <attributes-coll> |
| 121 |
; * - tests if the Node is an <Element> |
| 122 |
; *text* - tests if the Node is a text node |
| 123 |
; *PI* - tests if the Node is a PI node |
| 124 |
; *any* - #t for any type of Node |
| 125 |
|
| 126 |
(define (node-typeof? crit) |
| 127 |
(lambda (node) |
| 128 |
(case crit |
| 129 |
((*) (and (pair? node) (not (memq (car node) '(@ *PI*))))) |
| 130 |
((*any*) #t) |
| 131 |
((*text*) (string? node)) |
| 132 |
(else |
| 133 |
(and (pair? node) (eq? crit (car node)))) |
| 134 |
))) |
| 135 |
|
| 136 |
|
| 137 |
; The following procedure is the most generic transformation of SXML |
| 138 |
; into the corresponding XML document. The SXML tree is traversed |
| 139 |
; post-oder (depth-first) and transformed into another tree, which, |
| 140 |
; written in a depth-first fashion, results in an XML document. |
| 141 |
; Unlike the '(eval tree1)' example above, there is no need to declare |
| 142 |
; a specific binding for each tag beforehand. The computation below is |
| 143 |
; however very similar to call-by-value. Yet there is a twist: an |
| 144 |
; extension of bindings before argument expressions are evaluated. |
| 145 |
; The procedure below processes all elements and attributes in an SXML |
| 146 |
; tree uniformly. The example however can easily be amended to handle |
| 147 |
; a certain element in a particular way, or to omit it from the |
| 148 |
; resulting XML document. |
| 149 |
|
| 150 |
(SRV:send-reply |
| 151 |
(post-order tree1 |
| 152 |
`((@ |
| 153 |
((*default* ; local override for attributes |
| 154 |
. ,(lambda (attr-key . value) ((enattr attr-key) value)))) |
| 155 |
. ,(lambda (trigger . value) (list '@ value))) |
| 156 |
(*default* . ,(lambda (tag . elems) (apply (entag tag) elems))) |
| 157 |
(*text* . ,(lambda (trigger str) str)) |
| 158 |
)) |
| 159 |
) |
| 160 |
|
| 161 |
|
| 162 |
; Give the list of all href attributes in tree1 |
| 163 |
(pp |
| 164 |
(post-order tree1 |
| 165 |
`((href ((*text* . ,(lambda (trigger str) str))) |
| 166 |
. ,(lambda (trigger value) value)) |
| 167 |
(*text* . , (lambda (trigger value) '())) |
| 168 |
(*default* . ,(lambda (trigger . value) value)))) |
| 169 |
) |
| 170 |
|
| 171 |
(pp |
| 172 |
(post-order tree1 |
| 173 |
`((href ((*text* . ,(lambda (trigger str) str))) |
| 174 |
. ,(lambda (trigger . value) value)) |
| 175 |
(*text* . , (lambda (trigger value) '())) |
| 176 |
(*default* . ,(lambda (trigger . values) (apply append values))))) |
| 177 |
) |
| 178 |
|
| 179 |
|
| 180 |
; Pre-order traversal of a tree and creation of a new tree: |
| 181 |
; apply-templates:: tree x <templates> -> <new-tree> |
| 182 |
; where |
| 183 |
; <templates> ::= (<template> ...) |
| 184 |
; <template> ::= (<node-test> <node-test> ... <node-test> . <handler>) |
| 185 |
; <node-test> ::= an argument to node-typeof? above |
| 186 |
; <handler> ::= <tree> -> <new-tree> |
| 187 |
; |
| 188 |
; This procedure does a _normal_, pre-order traversal of an SXML |
| 189 |
; tree. It walks the tree, checking at each node against the list of |
| 190 |
; matching templates. |
| 191 |
; If the match is found (which must be unique, i.e., unambiguous), the |
| 192 |
; corresponding handler is invoked and given the current node as an |
| 193 |
; argument. The result from the handler, which must be a <tree>, |
| 194 |
; takes place of the current node in the resulting tree. |
| 195 |
; The name of the function is not accidental: it resembles rather closely |
| 196 |
; an 'apply-templates' function of XSLT. |
| 197 |
|
| 198 |
(define (apply-templates tree templates) |
| 199 |
|
| 200 |
; Filter the list of templates. If a template does not |
| 201 |
; contradict the given node (that is, its head matches |
| 202 |
; the type of the node), chop off the head and keep the |
| 203 |
; rest as the result. All contradicting templates are removed. |
| 204 |
(define (filter-templates node templates) |
| 205 |
(cond |
| 206 |
((null? templates) templates) |
| 207 |
((not (pair? (car templates))) ; A good template must be a list |
| 208 |
(filter-templates node (cdr templates))) |
| 209 |
(((node-typeof? (caar templates)) node) |
| 210 |
(cons (cdar templates) (filter-templates node (cdr templates)))) |
| 211 |
(else |
| 212 |
(filter-templates node (cdr templates))))) |
| 213 |
|
| 214 |
; Here <templates> ::= [<template> | <handler>] |
| 215 |
; If there is a <handler> in the above list, it must |
| 216 |
; be only one. If found, return it; otherwise, return #f |
| 217 |
(define (find-handler templates) |
| 218 |
(and (pair? templates) |
| 219 |
(cond |
| 220 |
((procedure? (car templates)) |
| 221 |
(if (find-handler (cdr templates)) |
| 222 |
(error "ambiguous template match")) |
| 223 |
(car templates)) |
| 224 |
(else (find-handler (cdr templates)))))) |
| 225 |
|
| 226 |
(let loop ((tree tree) (active-templates '())) |
| 227 |
;(cout "active-templates: " active-templates nl "tree: " tree nl) |
| 228 |
(if (nodeset? tree) |
| 229 |
(map-union (lambda (a-tree) (loop a-tree active-templates)) tree) |
| 230 |
(let ((still-active-templates |
| 231 |
(append |
| 232 |
(filter-templates tree active-templates) |
| 233 |
(filter-templates tree templates)))) |
| 234 |
(cond |
| 235 |
;((null? still-active-templates) '()) |
| 236 |
((find-handler still-active-templates) => |
| 237 |
(lambda (handler) (handler tree))) |
| 238 |
((not (pair? tree)) '()) |
| 239 |
(else |
| 240 |
(loop (cdr tree) still-active-templates)))))) |
| 241 |
) |
| 242 |
|
| 243 |
|
| 244 |
; Give the list of all td elements in tree1 |
| 245 |
(pp |
| 246 |
(apply-templates tree1 |
| 247 |
`((td . ,(lambda (node) node))) |
| 248 |
)) |
| 249 |
|
| 250 |
; Give the list of all href attributes in tree1 that are children |
| 251 |
; of 'a' elements |
| 252 |
(pp |
| 253 |
(apply-templates tree1 |
| 254 |
`((a @ href . ,(lambda (node) node))) |
| 255 |
)) |
| 256 |
|
| 257 |
; Give the list of all align attributes in tree1 that are children |
| 258 |
; of 'td' elements |
| 259 |
(pp |
| 260 |
(apply-templates tree1 |
| 261 |
`((td @ align . ,(lambda (node) node))) |
| 262 |
)) |
| 263 |
|
| 264 |
; Give the list of all Title attributes in tree2 that are children |
| 265 |
; of 'PERIOD' and 'VAR' elements |
| 266 |
(pp |
| 267 |
(apply-templates tree2 |
| 268 |
`((PERIOD @ Title *text* . ,(lambda (node) (list 'Title-PERIOD node))) |
| 269 |
(VAR @ Title *text* . ,(lambda (node) (list 'Title-VAR node)))) |
| 270 |
)) |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
(include "SXPath.scm") |
| 276 |
|
| 277 |
; XSLT processing in Scheme |
| 278 |
; The following Scheme code transforms the tree2 above the same way |
| 279 |
; as an XSL stylesheet |
| 280 |
; http://zowie.metnet.navy.mil/~spawar/JMV-TNG/XML/TC/taf.xsl |
| 281 |
; does regarding the corresponding OMF XML document. |
| 282 |
; |
| 283 |
; This XSLT stylesheet is commented in part below, for easy reference. |
| 284 |
|
| 285 |
; <xsl:template match="TAF"> |
| 286 |
; <P><BR/></P> |
| 287 |
; <TABLE BGCOLOR="#CCCCCC" width="100%" cellpadding="3"> |
| 288 |
; <TR> |
| 289 |
; <TD><xsl:value-of select="@SName"/></TD> |
| 290 |
; <TD>Id: <xsl:value-of select="@BId"/></TD> |
| 291 |
; <TD>[<xsl:value-of select="@LatLon"/>]</TD> |
| 292 |
; </TR></TABLE> |
| 293 |
|
| 294 |
; <TABLE width="100%" cellpadding="1" BORDER="1" RULES="none" FRAME="hsides"> |
| 295 |
; <TR> |
| 296 |
; <TD WIDTH="20%"> |
| 297 |
; <xsl:eval>conv_date(this.selectSingleNode('@TStamp').text)</xsl:eval> |
| 298 |
; <BR/> |
| 299 |
; <xsl:eval>conv_trange(this.selectSingleNode('VALID/@TRange').text)</xsl:eval> |
| 300 |
; </TD> |
| 301 |
; <TD> |
| 302 |
; <xsl:value-of select="VALID"/> |
| 303 |
; <xsl:apply-templates select="PERIOD/*"/> |
| 304 |
; </TD> |
| 305 |
; </TR> |
| 306 |
; </TABLE> |
| 307 |
; </xsl:template> |
| 308 |
|
| 309 |
; <xsl:template match="PERIOD/PREVAILING"> |
| 310 |
; <DIV> |
| 311 |
; <xsl:attribute name="class"> |
| 312 |
; <xsl:eval>period_class(this.selectSingleNode('../@TRange').text)</xsl:eval> |
| 313 |
; </xsl:attribute> |
| 314 |
; <xsl:if test="../@Title"><xsl:value-of select="../@Title"/> </xsl:if> |
| 315 |
; <xsl:value-of /> |
| 316 |
; </DIV> |
| 317 |
; </xsl:template> |
| 318 |
|
| 319 |
; <xsl:template match="PERIOD/VAR"> |
| 320 |
; <DIV class="var"> |
| 321 |
; <xsl:value-of select="@Title"/> <xsl:value-of /> |
| 322 |
; </DIV> |
| 323 |
; </xsl:template> |
| 324 |
|
| 325 |
; Scheme XSLT code follows. It does not need to resort to xsl:eval : |
| 326 |
; regular Scheme backquote suffices. The Scheme code is also shorter, |
| 327 |
; and well integrated. |
| 328 |
|
| 329 |
(OS:putenv "TZ" "GMT") |
| 330 |
|
| 331 |
; trange-str is a string of two numbers (epoch secs) |
| 332 |
; separated by a ", " |
| 333 |
; The following function returns the numbers as a pair |
| 334 |
(define (conv-TRange trange-str) |
| 335 |
(call-with-input-string trange-str |
| 336 |
(lambda (port) |
| 337 |
(let* ((tstart (read port)) |
| 338 |
(delim (read-char port)) |
| 339 |
(tend (read port))) |
| 340 |
(if (and (eq? delim #\,) (number? tstart) (number? tend)) |
| 341 |
(cons tstart tend) |
| 342 |
(error "Invalid Trange string: " trange-str)))))) |
| 343 |
|
| 344 |
|
| 345 |
(define (handle-TAF TAF-elem) |
| 346 |
(define time-current 958078810) ; should be (OS:time) |
| 347 |
; Handle PREVAILING grandchild of a TAF-elem |
| 348 |
(define (handle-Prevailing elem) |
| 349 |
(let ((trange (conv-TRange (car |
| 350 |
((sxpath `((PERIOD ((eq? ,elem))) @ TRange *text*)) |
| 351 |
TAF-elem))))) |
| 352 |
`(div (@ (class ,(if (<= (car trange) time-current (cdr trange)) |
| 353 |
"per_c" "per_nc"))) |
| 354 |
,((sxpath `((PERIOD ((eq? ,elem))) @ Title *text*)) TAF-elem) |
| 355 |
" " |
| 356 |
,((sxpath '(*text*)) elem)))) |
| 357 |
|
| 358 |
`((p (br)) |
| 359 |
(table (@ (bgcolor "#CCCCCC") (width "100%") (cellpadding "3")) |
| 360 |
(tr |
| 361 |
(td ,((sxpath '(@ SName *text*)) TAF-elem)) |
| 362 |
(td "Id: " ,((sxpath '(@ BId *text*)) TAF-elem)) |
| 363 |
(td "[" ,((sxpath '(@ LatLon *text*)) TAF-elem) "]") |
| 364 |
)) |
| 365 |
(table |
| 366 |
(@ (width "100%") (cellpadding "1") (BORDER "1") (RULES "none") |
| 367 |
(FRAME "hsides")) |
| 368 |
(tr (td (@ (width "20%")) |
| 369 |
,(OS:cftime "%m-%d %H:%M" |
| 370 |
(string->number |
| 371 |
(car ((sxpath '(@ TStamp *text*)) TAF-elem)))) |
| 372 |
(br) |
| 373 |
,(let ((trange |
| 374 |
(conv-TRange |
| 375 |
(car ((sxpath '(VALID @ TRange *text*)) TAF-elem))))) |
| 376 |
(list (OS:cftime "%d %H:%M" (car trange)) |
| 377 |
(OS:cftime " - %d %H:%M" (cdr trange)))) |
| 378 |
) |
| 379 |
(td ,((sxpath '(VALID *text*)) TAF-elem) |
| 380 |
,(apply-templates TAF-elem |
| 381 |
`((PERIOD PREVAILING . ,handle-Prevailing) |
| 382 |
(PERIOD VAR . ,(lambda (elem) |
| 383 |
`(div (@ (class "var")) |
| 384 |
,((sxpath '(@ Title *text*)) elem) |
| 385 |
" " |
| 386 |
,((sxpath '(*text*)) elem)))) |
| 387 |
)) |
| 388 |
) |
| 389 |
))) |
| 390 |
) |
| 391 |
|
| 392 |
|
| 393 |
(cerr "\n\nResult:\n") |
| 394 |
(pp |
| 395 |
(apply-templates tree2 |
| 396 |
`((TAF . ,handle-TAF)) |
| 397 |
)) |
| 398 |
|
| 399 |
(cerr "\n\nIn HTML:\n") |
| 400 |
(SRV:send-reply |
| 401 |
(post-order |
| 402 |
(apply-templates tree2 |
| 403 |
`((TAF . ,handle-TAF)) |
| 404 |
) |
| 405 |
|
| 406 |
`((@ |
| 407 |
((*default* ; local override for attributes |
| 408 |
. ,(lambda (attr-key value) ((enattr attr-key) value)))) |
| 409 |
. ,(lambda (trigger . value) (list '@ value))) |
| 410 |
(*default* . ,(lambda (tag . elems) (apply (entag tag) elems))) |
| 411 |
(*text* . ,(lambda (trigger str) str)) |
| 412 |
)) |
| 413 |
) |