| 1 |
;; S X M L T o o l s |
| 2 |
; $Revision: 1.1 $ from $Date: 2003-07-22 11:22:11 $: |
| 3 |
; |
| 4 |
; This software is in Public Domain. |
| 5 |
; IT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND. |
| 6 |
; |
| 7 |
; Please send bug reports and comments to [email protected] |
| 8 |
; Kirill Lisovsky |
| 9 |
; |
| 10 |
; SXML normal form used for normalization-dependent functions: |
| 11 |
; If attr-list is present it's always the second in SXML element. |
| 12 |
; If aux-list is present - then list of attributes is always |
| 13 |
; included, and aux-list is always the third. |
| 14 |
; Minimized form is just the same, but all the empty aux-lists are |
| 15 |
; absent, and empty attr-lists are present only in elements with aux-lists |
| 16 |
; present. |
| 17 |
|
| 18 |
;============================================================================== |
| 19 |
; Auxiliary functions. |
| 20 |
|
| 21 |
; unlike filter-map from SRFI-1 this function uses separate predicate |
| 22 |
; and mapping functions. |
| 23 |
; Applies proc to all the elements of source list that satisfy the predicate |
| 24 |
; and return the list of the results. |
| 25 |
(define (filter-and-map pred proc lis) |
| 26 |
(let rpt ((l lis)) |
| 27 |
(if (null? l) |
| 28 |
'() |
| 29 |
(if (pred (car l)) |
| 30 |
(cons (proc (car l)) (rpt (cdr l))) |
| 31 |
(rpt (cdr l)))))) |
| 32 |
|
| 33 |
; Applies pred to every member of lst and yields #t if all the results |
| 34 |
; are #t |
| 35 |
(define (check-list pred lst) |
| 36 |
(cond |
| 37 |
((null? lst) #t) |
| 38 |
((pred (car lst)) |
| 39 |
(check-list pred (cdr lst))) |
| 40 |
(else #f))) |
| 41 |
|
| 42 |
; Returns attr-list node for a given obj |
| 43 |
; or #f if it is absent |
| 44 |
(define (sxml:attr-list-node obj) |
| 45 |
(if (and (not (null? (cdr obj))) |
| 46 |
(pair? (cadr obj)) |
| 47 |
(eq? '@ (caadr obj))) |
| 48 |
(cadr obj) |
| 49 |
#f)) |
| 50 |
|
| 51 |
; Returns attr-list wrapped in list |
| 52 |
; or '((@)) if it is absent and aux-list is present |
| 53 |
; or '() if both lists are absent |
| 54 |
(define (sxml:attr-as-list obj) |
| 55 |
(cond |
| 56 |
((sxml:attr-list-node obj) |
| 57 |
=> list) |
| 58 |
((sxml:aux-list-node obj) |
| 59 |
'((@))) |
| 60 |
(else '()))) |
| 61 |
|
| 62 |
; Returns aux-list node for a given obj |
| 63 |
; or #f if it is absent |
| 64 |
(define (sxml:aux-list-node obj) |
| 65 |
(if |
| 66 |
(or (null? (cdr obj)) |
| 67 |
(null? (cddr obj)) |
| 68 |
(not (pair? (caddr obj))) |
| 69 |
(not (eq? (caaddr obj) '@@))) |
| 70 |
#f |
| 71 |
(caddr obj))) |
| 72 |
|
| 73 |
; Returns aux-list wrapped in list |
| 74 |
; or '() if it is absent |
| 75 |
(define (sxml:aux-as-list obj) |
| 76 |
(cond |
| 77 |
((sxml:aux-list-node obj) |
| 78 |
=> list) |
| 79 |
(else '()))) |
| 80 |
|
| 81 |
; optimized (string-rindex name #\:) |
| 82 |
; returns position of a separator between namespace-id and LocalName |
| 83 |
(define-macro (sxml:find-name-separator len) |
| 84 |
`(let rpt ((pos (-- ,len))) |
| 85 |
(cond |
| 86 |
((negative? pos) #f) |
| 87 |
((char=? #\: (string-ref name pos)) pos) |
| 88 |
(else (rpt (-- pos)))))) |
| 89 |
|
| 90 |
|
| 91 |
; sxml error message |
| 92 |
(define (sxml:error . messages) |
| 93 |
(cerr nl "SXML ERROR: ") |
| 94 |
(apply cerr messages) |
| 95 |
(cerr nl) |
| 96 |
(exit -1)) |
| 97 |
|
| 98 |
;============================================================================== |
| 99 |
; Predicates |
| 100 |
|
| 101 |
; Predicate which returns #t if given element <obj> is empty. |
| 102 |
; Empty element has no nested elements, text nodes, PIs, Comments or entities |
| 103 |
; but it may contain attributes or namespace-id. |
| 104 |
; It is a SXML counterpart of XML empty-element. |
| 105 |
(define (sxml:empty-element? obj) |
| 106 |
(not |
| 107 |
((select-first-kid |
| 108 |
(lambda(x) |
| 109 |
(or ((ntype-names?? '(*PI* *COMMENT* *ENTITY*)) x) |
| 110 |
((ntype?? '*) x) |
| 111 |
(string? x)))) obj))) |
| 112 |
|
| 113 |
; Returns #t if the given <obj> is shallow-normalized SXML element. |
| 114 |
; The element itself has to be normalised but its nested elements are not tested. |
| 115 |
(define (sxml:shallow-normalized? obj) |
| 116 |
(or |
| 117 |
(null? (cdr obj)) |
| 118 |
(and (or |
| 119 |
(and |
| 120 |
(pair? (cadr obj)) |
| 121 |
(eq? (caadr obj) '@)) |
| 122 |
(not ((select-first-kid (ntype-names?? '(@ @@))) obj))) |
| 123 |
(or (null? (cddr obj)) |
| 124 |
(and (pair? (caddr obj)) |
| 125 |
(eq? (caaddr obj) '@@)) |
| 126 |
(not ((select-first-kid (ntype?? '@@)) obj)))))) |
| 127 |
|
| 128 |
; Returns #t if the given <obj> is normalized SXML element. |
| 129 |
; The element itself and all its nested elements have to be normalised. |
| 130 |
(define (sxml:normalized? obj) |
| 131 |
(and |
| 132 |
(sxml:shallow-normalized? obj) |
| 133 |
(check-list |
| 134 |
(lambda(x) |
| 135 |
(if |
| 136 |
(sxml:element? x) |
| 137 |
(sxml:normalized? x) |
| 138 |
#t)) |
| 139 |
(sxml:content obj)) |
| 140 |
)) |
| 141 |
|
| 142 |
; Returns #t if the given <obj> is shallow-minimized SXML element. |
| 143 |
; The element itself has to be minimised but its nested elements are not tested. |
| 144 |
(define (sxml:shallow-minimized? obj) |
| 145 |
(and |
| 146 |
(sxml:shallow-normalized? obj) |
| 147 |
(not (and (sxml:aux-list-node obj) |
| 148 |
(null? (sxml:aux-list obj)))) |
| 149 |
(not (and (sxml:attr-list-node obj) |
| 150 |
(null? (sxml:attr-list obj)) |
| 151 |
(not (sxml:aux-list-node obj)))))) |
| 152 |
|
| 153 |
; Returns #t if the given <obj> is minimized SXML element. |
| 154 |
; The element itself and all its nested elements have to be minimised. |
| 155 |
(define (sxml:minimized? obj) |
| 156 |
(and |
| 157 |
(sxml:shallow-minimized? obj) |
| 158 |
(check-list |
| 159 |
(lambda(x) |
| 160 |
(if |
| 161 |
(sxml:element? x) |
| 162 |
(sxml:minimized? x) |
| 163 |
#t)) |
| 164 |
(sxml:content obj)) |
| 165 |
)) |
| 166 |
|
| 167 |
;============================================================================== |
| 168 |
; Accessors |
| 169 |
|
| 170 |
; Returns a name of a given SXML node |
| 171 |
; It is introduced for the sake of encapsulation. |
| 172 |
(define sxml:name car) |
| 173 |
|
| 174 |
; A version of sxml:name, which returns #f if the given <obj> is |
| 175 |
; not a SXML element. |
| 176 |
; Otherwise returns its name. |
| 177 |
(define (sxml:element-name obj) |
| 178 |
(and ((ntype?? '*) obj) |
| 179 |
(car obj))) |
| 180 |
|
| 181 |
; Safe version of sxml:name, which returns #f if the given <obj> is |
| 182 |
; not a SXML node. |
| 183 |
; Otherwise returns its name. |
| 184 |
(define (sxml:node-name obj) |
| 185 |
(and (pair? obj) |
| 186 |
(symbol? (car obj)) |
| 187 |
(car obj))) |
| 188 |
|
| 189 |
; Returns Local Part of Qualified Name (Namespaces in XML production [6]) |
| 190 |
; for given obj, which is ":"-separated suffix of its Qualified Name |
| 191 |
; If a name of a node given is NCName (Namespaces in XML production [4]), then |
| 192 |
; it is returned as is. |
| 193 |
; Please note that while SXML name is a symbol this function returns a string. |
| 194 |
(define (sxml:ncname obj) |
| 195 |
(let* ((name (symbol->string (car obj))) |
| 196 |
(len (string-length name))) |
| 197 |
(cond |
| 198 |
((sxml:find-name-separator len) |
| 199 |
=> (lambda (pos) |
| 200 |
(substring name (+ pos 1) len))) |
| 201 |
(else name)))) |
| 202 |
|
| 203 |
; Returns namespace-id part of given name, or #f if it's LocalName |
| 204 |
(define (sxml:name->ns-id sxml-name) |
| 205 |
(let* ((name (symbol->string sxml-name))) |
| 206 |
(cond |
| 207 |
((sxml:find-name-separator (string-length name)) |
| 208 |
=> (lambda (pos) |
| 209 |
(substring name 0 pos))) |
| 210 |
(else #f)))) |
| 211 |
|
| 212 |
|
| 213 |
; Returns the content of given SXML element or nodeset (just text and element |
| 214 |
; nodes) representing it as a list of strings and nested elements in document |
| 215 |
; order. This list is empty if <obj> is empty element or empty list. |
| 216 |
(define (sxml:content obj) |
| 217 |
(((if (nodeset? obj) |
| 218 |
sxml:filter |
| 219 |
select-kids) |
| 220 |
(lambda(x) |
| 221 |
(or |
| 222 |
(string? x) ; ((ntype?? '*text*) x) |
| 223 |
((ntype?? '*) x)))) |
| 224 |
obj)) |
| 225 |
|
| 226 |
;------------------------------------------------------------------------------ |
| 227 |
; Normalization-dependent accessors |
| 228 |
; |
| 229 |
; |
| 230 |
; "Universal" accessors are less effective but may be used for non-normalized SXML |
| 231 |
; Safe accessors are named with suffix '-u' |
| 232 |
; |
| 233 |
; "Fast" accessors are optimized for normalized SXML data. |
| 234 |
; They are not applicable to arbitrary non-normalized SXML data |
| 235 |
; Their names has no specific suffixes |
| 236 |
|
| 237 |
; Returns all the content of normalized SXML element except attr-list and |
| 238 |
; aux-list. |
| 239 |
; Thus it includes PI, COMMENT and ENTITY nodes as well as TEXT and ELEMENT nodes |
| 240 |
; returned by sxml:content. |
| 241 |
; Returns a list of nodes in document order or empty list if <obj> is empty |
| 242 |
; element or empty list. |
| 243 |
; This function is faster than sxml:content |
| 244 |
(define (sxml:content-raw obj) |
| 245 |
((if (and (not (null? (cdr obj))) |
| 246 |
(pair? (cadr obj)) (eq? (caadr obj) '@)) |
| 247 |
(if (and (not (null? (cddr obj))) |
| 248 |
(pair? (caddr obj)) (eq? (caaddr obj) '@@)) |
| 249 |
cdddr |
| 250 |
cddr) |
| 251 |
cdr) obj)) |
| 252 |
|
| 253 |
|
| 254 |
; Returns the list of attributes for given element or nodeset. |
| 255 |
; Analog of ((sxpath '(@ *)) obj) |
| 256 |
; Empty list is returned if there is no list of attributes. |
| 257 |
(define (sxml:attr-list-u obj) |
| 258 |
(cond (((select-first-kid (ntype?? '@)) obj) |
| 259 |
=> cdr) |
| 260 |
(else '()))) |
| 261 |
|
| 262 |
; Returns the list of auxiliary nodes for given element or nodeset. |
| 263 |
; Analog of ((sxpath '(@@ *)) obj) |
| 264 |
; Empty list is returned if a list of auxiliary nodes is absent. |
| 265 |
(define (sxml:aux-list obj) |
| 266 |
(if |
| 267 |
(or (null? (cdr obj)) |
| 268 |
(null? (cddr obj)) |
| 269 |
(not (pair? (caddr obj))) |
| 270 |
(not (eq? (caaddr obj) '@@))) |
| 271 |
'() |
| 272 |
(cdaddr obj))) |
| 273 |
|
| 274 |
; Returns the list of auxiliary nodes for given element or nodeset. |
| 275 |
; Analog of ((sxpath '(@@ *)) obj) |
| 276 |
; Empty list is returned if a list of auxiliary nodes is absent. |
| 277 |
(define (sxml:aux-list-u obj) |
| 278 |
(cond (((select-first-kid (ntype?? '@@)) obj) |
| 279 |
=> cdr) |
| 280 |
(else '()))) |
| 281 |
|
| 282 |
; Return the first aux-node with <aux-name> given in SXML element <obj> |
| 283 |
; or #f is such a node is absent. |
| 284 |
; NOTE: it returns just the FIRST node found even if multiple nodes are |
| 285 |
; present, so it's mostly intended for nodes with unique names |
| 286 |
(define (sxml:aux-node obj aux-name) |
| 287 |
(cond |
| 288 |
((assq aux-name (sxml:aux-list obj))) |
| 289 |
(else #f))) |
| 290 |
|
| 291 |
; Return a list of aux-node with <aux-name> given in SXML element <obj> |
| 292 |
; or '() if such a node is absent. |
| 293 |
(define (sxml:aux-nodes obj aux-name) |
| 294 |
(filter |
| 295 |
(lambda(x) (eq? aux-name (car x))) |
| 296 |
(sxml:aux-list obj))) |
| 297 |
|
| 298 |
; Accessor for an attribute <attr-name> of given SXML element <obj> which |
| 299 |
; It returns: |
| 300 |
; the value of the attribute if the attribute is present |
| 301 |
; #f if there is no such an attribute in the given element |
| 302 |
(define (sxml:attr obj attr-name) |
| 303 |
(cond |
| 304 |
((assq attr-name (sxml:attr-list obj)) |
| 305 |
=> cadr) |
| 306 |
(else #f))) |
| 307 |
|
| 308 |
|
| 309 |
; Accessor for a numerical attribute <attr-name> of given SXML element <obj> |
| 310 |
; which It returns: |
| 311 |
; a value of the attribute as the attribute as a number if the attribute |
| 312 |
; is present and its value may be converted to number using string->number |
| 313 |
; #f if there is no such an attribute in the given element or |
| 314 |
; its value can't be converted to a number |
| 315 |
(define (sxml:num-attr obj attr-name) |
| 316 |
(cond |
| 317 |
((assq attr-name (sxml:attr-list obj)) |
| 318 |
=> (lambda(x) (string->number (cadr x)))) |
| 319 |
(else #f))) |
| 320 |
|
| 321 |
; Accessor for an attribute <attr-name> of given SXML element <obj> which |
| 322 |
; may also be an attributes-list or nodeset (usually content of SXML element) |
| 323 |
; |
| 324 |
; It returns: |
| 325 |
; the value of the attribute if the attribute is present |
| 326 |
; #f if there is no such an attribute in the given element |
| 327 |
(define (sxml:attr-u obj attr-name) |
| 328 |
(cond |
| 329 |
((assq attr-name |
| 330 |
; the list of attributes is computed below |
| 331 |
(cond |
| 332 |
((and (not (null? (cdr obj))) |
| 333 |
(pair? (cadr obj)) |
| 334 |
(eq? '@ (caadr obj))) |
| 335 |
(cdadr obj)) ; fast track for normalized elements |
| 336 |
((eq? '@ (car obj)) |
| 337 |
(cdr obj)) ; if applied to attr-list |
| 338 |
(else (sxml:attr-list-u obj)))) |
| 339 |
=> cadr) |
| 340 |
(else #f))) |
| 341 |
|
| 342 |
; Returns the list of namespaces for given element. |
| 343 |
; Analog of ((sxpath '(@@ *NAMESPACES* *)) obj) |
| 344 |
; Empty list is returned if there is no list of namespaces. |
| 345 |
(define (sxml:ns-list obj) |
| 346 |
(cond ((assv '*NAMESPACES* (sxml:aux-list obj)) |
| 347 |
=> cdr) |
| 348 |
(else '()))) |
| 349 |
|
| 350 |
; Returns the list of namespace-assoc's for given namespace-id in |
| 351 |
; SXML element <obj>. |
| 352 |
; Analog of ((sxpath '(@@ *NAMESPACES* namespace-id)) obj) |
| 353 |
; Empty list is returned if there is no namespace-assoc with namespace-id |
| 354 |
; given. |
| 355 |
(define (sxml:ns-id->nodes obj namespace-id) |
| 356 |
(filter |
| 357 |
(lambda(x) |
| 358 |
(eq? (car x) namespace-id)) |
| 359 |
(sxml:ns-list obj))) |
| 360 |
|
| 361 |
; It returns: |
| 362 |
; A URI's for namespace-id given |
| 363 |
; #f if there is no namespace-assoc with namespace-id given |
| 364 |
(define (sxml:ns-id->uri obj namespace-id) |
| 365 |
(cond |
| 366 |
((assq namespace-id (sxml:ns-list obj)) |
| 367 |
=> cadr) |
| 368 |
(else #f))) |
| 369 |
|
| 370 |
; Returns a list of namespace-assocs nodes for NS URI given |
| 371 |
(define (sxml:ns-uri->nodes obj URI) |
| 372 |
(filter |
| 373 |
(lambda (ns-assoc) |
| 374 |
(string=? (cadr ns-assoc) URI)) |
| 375 |
(sxml:ns-list obj))) |
| 376 |
|
| 377 |
; Returns a namespace-id for NS URI given |
| 378 |
(define (sxml:ns-uri->id obj URI) |
| 379 |
(let rpt ((ns-assocs (sxml:ns-list obj))) |
| 380 |
(cond |
| 381 |
((null? ns-assocs) #f) |
| 382 |
((string=? (cadar ns-assocs) URI) |
| 383 |
(caar ns-assocs)) |
| 384 |
(else (rpt (cdr ns-assocs))) |
| 385 |
))) |
| 386 |
|
| 387 |
; Returns namespace-id for given namespace-assoc list |
| 388 |
(define sxml:ns-id car) |
| 389 |
|
| 390 |
; Returns URI for given namespace-assoc list |
| 391 |
(define sxml:ns-uri cadr) |
| 392 |
|
| 393 |
; It returns namespace prefix for given namespace-assoc list |
| 394 |
; Original (as in XML document) prefix for namespace-id given |
| 395 |
; has to be strored as the third element in namespace-assoc list |
| 396 |
; if it is different from namespace-id. |
| 397 |
; If original prefix is omitted in namespace-assoc then |
| 398 |
; namespace-id is used instead |
| 399 |
(define (sxml:ns-prefix ns-assoc) |
| 400 |
(if (> (length ns-assoc) 2) |
| 401 |
(caddr ns-assoc) |
| 402 |
(car ns-assoc))) |
| 403 |
|
| 404 |
;============================================================================== |
| 405 |
; Data modification functions |
| 406 |
; Constructors and mutators for normalized SXML data |
| 407 |
; |
| 408 |
; This functions are optimized for normalized SXML data. |
| 409 |
; They are not applicable to arbitrary non-normalized SXML data |
| 410 |
; |
| 411 |
; Most of the functions are provided in two variants: |
| 412 |
; 1. side-effect intended functions for linear update of given elements. |
| 413 |
; Their names are ended with exclamation mark. |
| 414 |
; An example: |
| 415 |
; sxml:change-content! |
| 416 |
; 2. pure functions without side-effects which return modified elements. |
| 417 |
; An example: |
| 418 |
; sxml:change-content |
| 419 |
|
| 420 |
; Change the content of given SXML element to <new-content> |
| 421 |
; If <new-content> is an empty list then the <obj> is transformed |
| 422 |
; The resulting SXML element is normalized |
| 423 |
; Former name sxml:content! |
| 424 |
(define (sxml:change-content! obj new-content) |
| 425 |
(set-cdr! obj |
| 426 |
`( |
| 427 |
,@(sxml:attr-as-list obj) |
| 428 |
,@(sxml:aux-as-list obj) |
| 429 |
,@new-content))) |
| 430 |
|
| 431 |
; Change the content of given SXML element to <new-content> |
| 432 |
; If <new-content> is an empty list then the <obj> is transformed |
| 433 |
; to an empty element |
| 434 |
; The resulting SXML element is normalized |
| 435 |
(define (sxml:change-content obj new-content) |
| 436 |
`(,(sxml:name obj) |
| 437 |
,@(sxml:attr-as-list obj) |
| 438 |
,@(sxml:aux-as-list obj) |
| 439 |
,@new-content)) |
| 440 |
|
| 441 |
; The resulting SXML element is normalized, if <new-attrlist> is empty, |
| 442 |
; the cadr of <obj> is (@) |
| 443 |
(define (sxml:change-attrlist obj new-attrlist) |
| 444 |
`(,(sxml:name obj) |
| 445 |
,@(cond |
| 446 |
(new-attrlist |
| 447 |
`((@ ,@new-attrlist))) |
| 448 |
((sxml:aux-list-node obj) |
| 449 |
'((@))) |
| 450 |
(else `())) |
| 451 |
,@(sxml:aux-as-list obj) |
| 452 |
,@(sxml:content obj))) |
| 453 |
|
| 454 |
; The resulting SXML element is normalized, if <new-attrlist> is empty, |
| 455 |
; the cadr of <obj> is (@) |
| 456 |
; Former name sxml:attrlist! |
| 457 |
(define (sxml:change-attrlist! obj new-attrlist) |
| 458 |
(set-cdr! obj |
| 459 |
`( |
| 460 |
,@(cond |
| 461 |
(new-attrlist |
| 462 |
`((@ ,@new-attrlist))) |
| 463 |
((sxml:aux-list-node obj) |
| 464 |
'((@))) |
| 465 |
(else `())) |
| 466 |
,@(sxml:aux-as-list obj) |
| 467 |
,@(sxml:content obj)))) |
| 468 |
|
| 469 |
; Change a name of SXML element destructively |
| 470 |
; Former name was 'sxml:name!' |
| 471 |
(define (sxml:change-name! obj new-name) |
| 472 |
(set-car! obj new-name)) |
| 473 |
|
| 474 |
; Returns SXML element with its name changed |
| 475 |
(define (sxml:change-name obj new-name) |
| 476 |
(cons new-name (cdr obj))) |
| 477 |
|
| 478 |
; Returns SXML element <obj> with attribute <attr> added or #f |
| 479 |
; if the attribute with given name already exists, |
| 480 |
; <attr> is (<attr-name> <attr-value>) |
| 481 |
; Pure functional counterpart to sxml:add-attr! |
| 482 |
(define (sxml:add-attr obj attr) |
| 483 |
(let ((attr-list (sxml:attr-list obj))) |
| 484 |
(if (assq (car attr) attr-list) |
| 485 |
#f |
| 486 |
`(,(sxml:name obj) |
| 487 |
(@ ,@(cons attr attr-list)) |
| 488 |
,@(sxml:aux-as-list obj) |
| 489 |
,@(sxml:content obj))))) |
| 490 |
|
| 491 |
; Add an attribute <attr> for an element <obj> |
| 492 |
; Returns #f if the attribute with given name already exists. |
| 493 |
; The resulting SXML node is normalized. |
| 494 |
; Linear update counterpart to sxml:add-attr |
| 495 |
(define (sxml:add-attr! obj attr) |
| 496 |
(let ((attr-list (sxml:attr-list obj))) |
| 497 |
(if (assq (car attr) attr-list) |
| 498 |
#f |
| 499 |
(begin |
| 500 |
(set-cdr! obj |
| 501 |
`( |
| 502 |
(@ ,@(cons attr attr-list)) |
| 503 |
,@(sxml:aux-as-list obj) |
| 504 |
,@(sxml:content obj))) |
| 505 |
obj)))) |
| 506 |
|
| 507 |
|
| 508 |
; Returns SXML element <obj> with changed value of attribute <attr> or #f |
| 509 |
; if where is no attribute with given name. |
| 510 |
; <attr> is (<attr-name> <attr-value>) |
| 511 |
(define (sxml:change-attr obj attr) |
| 512 |
(let ((attr-list (sxml:attr-list obj))) |
| 513 |
(if (null? attr-list) |
| 514 |
#f |
| 515 |
(cond |
| 516 |
((assv (car attr) attr-list) |
| 517 |
=> (lambda (y) |
| 518 |
`(,(sxml:name obj) |
| 519 |
(@ ,@(map |
| 520 |
(lambda(at) |
| 521 |
(if |
| 522 |
(eq? at y) |
| 523 |
attr |
| 524 |
at)) |
| 525 |
attr-list)) |
| 526 |
,@(sxml:aux-as-list obj) |
| 527 |
,@(sxml:content obj) |
| 528 |
))) |
| 529 |
(else #f))))) |
| 530 |
|
| 531 |
; Change value of the attribute for element <obj> |
| 532 |
; <attr> is (<attr-name> <attr-value>) |
| 533 |
; Returns #f if where is no such attribute |
| 534 |
(define (sxml:change-attr! obj attr) |
| 535 |
(let ((x (sxml:attr-list obj))) |
| 536 |
(if (null? x) |
| 537 |
#f |
| 538 |
(cond |
| 539 |
((assv (car attr) x) => (lambda (y) |
| 540 |
(set-cdr! y (cdr attr)) obj)) |
| 541 |
(else #f))))) |
| 542 |
|
| 543 |
; Set attribute <attr> of element <obj> |
| 544 |
; If there is no such attribute the new one is added |
| 545 |
(define (sxml:set-attr obj attr) |
| 546 |
(let ((attr-list (sxml:attr-list obj))) |
| 547 |
(cond |
| 548 |
((assv (car attr) attr-list) |
| 549 |
=> (lambda (y) |
| 550 |
`(,(sxml:name obj) |
| 551 |
(@ ,@(map |
| 552 |
(lambda(at) |
| 553 |
(if |
| 554 |
(eq? at y) |
| 555 |
attr |
| 556 |
at)) |
| 557 |
attr-list)) |
| 558 |
,@(sxml:aux-as-list obj) |
| 559 |
,@(sxml:content obj) |
| 560 |
))) |
| 561 |
(else |
| 562 |
`(,(sxml:name obj) |
| 563 |
(@ ,@(cons attr attr-list)) |
| 564 |
,@(sxml:aux-as-list obj) |
| 565 |
,@(sxml:content obj)))) |
| 566 |
)) |
| 567 |
|
| 568 |
; Set attribute <attr> of element <obj> |
| 569 |
; If there is no such attribute the new one is added |
| 570 |
(define (sxml:set-attr! obj attr) |
| 571 |
(let ((attr-list (sxml:attr-list obj))) |
| 572 |
(cond |
| 573 |
((assv (car attr) attr-list) |
| 574 |
=> (lambda (x) (set-cdr! x (cdr attr)))) |
| 575 |
(else (set-cdr! obj |
| 576 |
`((@ ,@(cons attr attr-list)) |
| 577 |
,@(sxml:aux-as-list obj) |
| 578 |
,@(sxml:content obj)))) |
| 579 |
))) |
| 580 |
|
| 581 |
; Returns SXML element <obj> with an auxiliary node <aux-node> added |
| 582 |
(define (sxml:add-aux obj aux-node) |
| 583 |
`(,(sxml:name obj) |
| 584 |
(@ ,@(sxml:attr-list obj)) |
| 585 |
(@@ ,@(cons aux-node (sxml:aux-list obj))) |
| 586 |
,@(sxml:content obj))) |
| 587 |
|
| 588 |
; Add an auxiliary node <aux-node> for an element <obj> |
| 589 |
(define (sxml:add-aux! obj aux-node) |
| 590 |
(set-cdr! obj |
| 591 |
`( |
| 592 |
(@ ,@(sxml:attr-list obj)) |
| 593 |
(@@ ,@(cons aux-node (sxml:aux-list obj))) |
| 594 |
,@(sxml:content obj))) |
| 595 |
obj) |
| 596 |
|
| 597 |
; Eliminates empty lists of attributes and aux-lists for given SXML element |
| 598 |
; <obj> and its descendants ("minimize" it) |
| 599 |
; Returns: minimized and normalized SXML element |
| 600 |
(define (sxml:squeeze! obj) |
| 601 |
(set-cdr! obj |
| 602 |
`(,@(cond |
| 603 |
((sxml:attr-list-node obj) |
| 604 |
=> (lambda (atl) |
| 605 |
(if (and (null? (cdr atl)) |
| 606 |
(null? (sxml:aux-list obj))) |
| 607 |
'() |
| 608 |
(list atl)))) |
| 609 |
(else '())) |
| 610 |
,@(cond ((sxml:aux-list-node obj) |
| 611 |
=> (lambda (axl) |
| 612 |
(if (null? (cdr axl)) |
| 613 |
'() |
| 614 |
(list axl)))) |
| 615 |
(else '())) |
| 616 |
,@(map |
| 617 |
(lambda(x) |
| 618 |
(cond |
| 619 |
(((ntype?? '*) x) |
| 620 |
(sxml:squeeze! x) |
| 621 |
x) |
| 622 |
(else x))) |
| 623 |
(sxml:content obj)) |
| 624 |
)) |
| 625 |
) |
| 626 |
|
| 627 |
|
| 628 |
; Eliminates empty lists of attributes and aux-lists for given SXML element |
| 629 |
; <obj> and its descendants ("minimize" it) |
| 630 |
; Returns: minimized and normalized SXML element |
| 631 |
(define (sxml:squeeze obj) |
| 632 |
`(,(sxml:name obj) |
| 633 |
,@(cond |
| 634 |
((sxml:attr-list-node obj) |
| 635 |
=> (lambda (atl) |
| 636 |
(if (and (null? (cdr atl)) |
| 637 |
(null? (sxml:aux-list obj))) |
| 638 |
'() |
| 639 |
(list atl)))) |
| 640 |
(else '())) |
| 641 |
,@(cond ((sxml:aux-list-node obj) |
| 642 |
=> (lambda (axl) |
| 643 |
(if (null? (cdr axl)) |
| 644 |
'() |
| 645 |
(list axl)))) |
| 646 |
(else '())) |
| 647 |
,@(map |
| 648 |
(lambda(x) |
| 649 |
(cond |
| 650 |
(((ntype?? '*) x) |
| 651 |
(sxml:squeeze x)) |
| 652 |
(else x))) |
| 653 |
(sxml:content obj)))) |
| 654 |
|
| 655 |
; Eliminates empty lists of attributes and ALL aux-lists for given SXML element |
| 656 |
; <obj> and its descendants |
| 657 |
; Returns: minimized and normalized SXML element |
| 658 |
(define (sxml:clean obj) |
| 659 |
`(,(sxml:name obj) |
| 660 |
,@(cond |
| 661 |
((sxml:attr-list-node obj) |
| 662 |
=> (lambda (atl) |
| 663 |
(if (null? (cdr atl)) |
| 664 |
'() |
| 665 |
(list atl)))) |
| 666 |
(else '())) |
| 667 |
,@(map |
| 668 |
(lambda(x) |
| 669 |
(cond |
| 670 |
(((ntype?? '*) x) |
| 671 |
(sxml:clean x)) |
| 672 |
(else x))) |
| 673 |
(sxml:content obj)))) |
| 674 |
;============================================================================== |
| 675 |
; SXPath-related |
| 676 |
|
| 677 |
;------------------------------------------------------------------------------ |
| 678 |
; Extensions |
| 679 |
|
| 680 |
; select-first-kid:: Pred -> Node -> Node |
| 681 |
; Given a Node, return its first child that satisfy |
| 682 |
; the test-pred? |
| 683 |
; Returns #f if there is no such a child |
| 684 |
; select-first-kid:: Pred -> Nodeset -> Node |
| 685 |
; The same as above, but select among children of all the nodes in |
| 686 |
; the Nodeset |
| 687 |
(define (select-first-kid test-pred?) |
| 688 |
(lambda(obj) |
| 689 |
(let rpt ((lst (if (symbol? (car obj)) |
| 690 |
(cdr obj) |
| 691 |
obj))) |
| 692 |
(cond |
| 693 |
((null? lst) #f) |
| 694 |
((and (pair? (car lst)) |
| 695 |
(test-pred? (car lst))) |
| 696 |
(car lst)) |
| 697 |
(else (rpt (cdr lst)))) |
| 698 |
))) |
| 699 |
|
| 700 |
;------------------------------------------------------------------------------ |
| 701 |
; Fast node-parent |
| 702 |
|
| 703 |
; Returns a function of one argument - SXML element - which returns its parent |
| 704 |
; node using *PARENT* pointer in aux-list |
| 705 |
; '*TOP-PTR* may be used as a pointer to root node |
| 706 |
; It return an empty list when applyed to root node |
| 707 |
(define (sxml:node-parent rootnode) |
| 708 |
(lambda(obj) |
| 709 |
(cond |
| 710 |
((sxml:aux-node obj '*PARENT*) |
| 711 |
=> (lambda(x) |
| 712 |
(if |
| 713 |
(eq? '*TOP-PTR* (cadr x)) |
| 714 |
rootnode |
| 715 |
((cadr x))))) |
| 716 |
((and (pair? obj) |
| 717 |
(eq? (car obj) '*TOP* )) |
| 718 |
'()) |
| 719 |
(else (sxml:error nl "PARENT pointer is absent in: " obj nl) |
| 720 |
)))) |
| 721 |
|
| 722 |
(define (sxml:add-parents obj . top-ptr) |
| 723 |
(let rpt |
| 724 |
((elt obj) |
| 725 |
(p '*TOP*) |
| 726 |
(at-aux (if (eq? (sxml:name obj) '*TOP*) |
| 727 |
(list (cons '@@ (sxml:aux-list-u obj))) |
| 728 |
(list |
| 729 |
(cons '@ (sxml:attr-list obj)) |
| 730 |
(cons '@@ (cons `(*PARENT* ,(lambda() (car top-ptr))) |
| 731 |
(sxml:aux-list obj)))))) |
| 732 |
) ; *TOP* is a parent for top-level element |
| 733 |
(let* ((h (list (sxml:name elt))) |
| 734 |
(b (append |
| 735 |
at-aux |
| 736 |
(map |
| 737 |
(lambda(x) |
| 738 |
(cond |
| 739 |
(((ntype?? '*) x) |
| 740 |
(rpt x h |
| 741 |
(list |
| 742 |
(cons '@ (sxml:attr-list x)) |
| 743 |
(cons '@@ (cons `(*PARENT* ,(lambda() h)) |
| 744 |
(sxml:aux-list x)))) |
| 745 |
)) |
| 746 |
(else x))) |
| 747 |
(sxml:content elt))))) |
| 748 |
(set-cdr! h b) |
| 749 |
h))) |
| 750 |
|
| 751 |
|
| 752 |
; Lookup an element using its ID |
| 753 |
(define (sxml:lookup id index) |
| 754 |
(cond |
| 755 |
((assoc id index) |
| 756 |
=> cdr) |
| 757 |
(else #f))) |
| 758 |
|
| 759 |
;============================================================================== |
| 760 |
; Markup generation |
| 761 |
|
| 762 |
; Filter the 'fragments' |
| 763 |
; The fragments are a list of strings, characters, |
| 764 |
; numbers, thunks, #f -- and other fragments. |
| 765 |
; The function traverses the tree depth-first, and returns a list |
| 766 |
; of strings, characters and executed thunks, and ignores #f and '(). |
| 767 |
; |
| 768 |
; If all the meaningful fragments are strings, then |
| 769 |
; (apply string-append ... ) |
| 770 |
; to a result of this function will return its string-value |
| 771 |
; |
| 772 |
; It may be considered as a variant of Oleg Kiselyov's SRV:send-reply: |
| 773 |
; While SRV:send-reply displays fragments, this function returns the list |
| 774 |
; of meaningful fragments and filter out the garbage. |
| 775 |
(define (sxml:clean-feed . fragments) |
| 776 |
(reverse |
| 777 |
(let loop ((fragments fragments) (result '())) |
| 778 |
(cond |
| 779 |
((null? fragments) result) |
| 780 |
((not (car fragments)) (loop (cdr fragments) result)) |
| 781 |
((null? (car fragments)) (loop (cdr fragments) result)) |
| 782 |
((pair? (car fragments)) |
| 783 |
(loop (cdr fragments) |
| 784 |
(loop (car fragments) result))) |
| 785 |
(else |
| 786 |
(loop (cdr fragments) |
| 787 |
(cons (car fragments) result))))))) |
| 788 |
|
| 789 |
;------------------------------------------------------------------------------ |
| 790 |
; XML |
| 791 |
|
| 792 |
; Creates the XML markup for attributes. |
| 793 |
(define (sxml:attr->xml attr) |
| 794 |
(list " " (sxml:ncname attr) |
| 795 |
"='" (cadr attr) "'")) |
| 796 |
|
| 797 |
; Return a string or a list of strings where all the occurences of |
| 798 |
; characters < > & " ' in a given string are replaced by corresponding |
| 799 |
; character entity references. See also: sxml:string->html |
| 800 |
(define sxml:string->xml |
| 801 |
(make-char-quotator |
| 802 |
'((#\< . "<") (#\> . ">") (#\& . "&") |
| 803 |
(#\" . """) (#\' . "'")))) |
| 804 |
|
| 805 |
; A version of dispatch-node specialized and optimized for SXML->XML |
| 806 |
; transformation. |
| 807 |
(define (sxml:sxml->xml tree) |
| 808 |
(cond |
| 809 |
((nodeset? tree) |
| 810 |
(map (lambda (a-tree) |
| 811 |
(sxml:sxml->xml a-tree)) |
| 812 |
tree)) |
| 813 |
((pair? tree) |
| 814 |
(let* ((name (sxml:name tree)) ; NS (URI-prefixed) not supported |
| 815 |
(nm (symbol->string name)) |
| 816 |
(content (sxml:content-raw tree))) |
| 817 |
`("<" ,nm ,@(map sxml:attr->xml (sxml:attr-list tree)) |
| 818 |
,@(if (null? content) '("/>") |
| 819 |
`(">" ,@(sxml:sxml->xml content) "</" ,nm ">"))))) |
| 820 |
((string? tree) (sxml:string->xml tree)) ; *text* |
| 821 |
(else (sxml:error "sxml->html - unexpected type of node: " tree)))) |
| 822 |
|
| 823 |
|
| 824 |
;------------------------------------------------------------------------------ |
| 825 |
; HTML |
| 826 |
|
| 827 |
; Creates the HTML markup for attributes. |
| 828 |
(define (sxml:attr->html attr) |
| 829 |
(if (equal? "" (cadr attr)) |
| 830 |
(list " " (sxml:ncname attr)) |
| 831 |
(list " " (sxml:ncname attr) "='" (cadr attr) "'"))) |
| 832 |
|
| 833 |
; Given a string, check to make sure it does not contain characters |
| 834 |
; < > & " that require encoding. Return either the original |
| 835 |
; string, or a list of string fragments with special characters |
| 836 |
; replaced by appropriate character entities. |
| 837 |
; Borrowed from Oleg Kiselyov's XML-to-HTML.scm (where its name is |
| 838 |
; string->goodHTML) |
| 839 |
(define sxml:string->html |
| 840 |
(make-char-quotator |
| 841 |
'((#\< . "<") (#\> . ">") (#\& . "&") (#\" . """)))) |
| 842 |
|
| 843 |
|
| 844 |
; This predicate yields #t for "non-terminated" HTML 4.0 tags |
| 845 |
(define (sxml:non-terminated-html-tag? tag) |
| 846 |
(memq tag |
| 847 |
'(area base basefont br col frame hr img input isindex link meta param))) |
| 848 |
|
| 849 |
|
| 850 |
; A version of dispatch-node specialized and optimized for SXML->HTML |
| 851 |
; transformation. |
| 852 |
(define (sxml:sxml->html tree) |
| 853 |
(cond |
| 854 |
((nodeset? tree) |
| 855 |
(map (lambda (a-tree) |
| 856 |
(sxml:sxml->html a-tree)) |
| 857 |
tree)) |
| 858 |
((pair? tree) |
| 859 |
(let* ((name (sxml:name tree)) |
| 860 |
(nm (symbol->string name)) |
| 861 |
(content (sxml:content-raw tree))) |
| 862 |
`("<" ,nm ,@(map sxml:attr->html (sxml:attr-list tree)) |
| 863 |
,@(if (null? content) |
| 864 |
(if (sxml:non-terminated-html-tag? name) '(">") '("/>")) |
| 865 |
`(">" ,@(sxml:sxml->html content) "</" ,nm ">"))))) |
| 866 |
((string? tree) (sxml:string->html tree)) ; *text* |
| 867 |
(else (sxml:error "sxml->html - unexpected type of node: " tree)))) |
| 868 |
|