| 1 |
;; $Id: sxpath.scm,v 1.1 2003-07-22 11:22:11 shirok Exp $ |
| 2 |
;; Highghest level SXPath |
| 3 |
;; Refactored from sxml-tools.scm and sxpathlib.scm |
| 4 |
|
| 5 |
;============================================================================== |
| 6 |
; Abbreviated SXPath |
| 7 |
|
| 8 |
; Evaluate an abbreviated SXPath |
| 9 |
; sxpath:: AbbrPath -> Converter, or |
| 10 |
; sxpath:: AbbrPath -> Node|Nodeset -> Nodeset |
| 11 |
; AbbrPath is a list. It is translated to the full SXPath according |
| 12 |
; to the following rewriting rules |
| 13 |
; (sxpath '()) -> (node-join) |
| 14 |
; (sxpath '(path-component ...)) -> |
| 15 |
; (node-join (sxpath1 path-component) (sxpath '(...))) |
| 16 |
; (sxpath1 '//) -> (node-or |
| 17 |
; (node-self (ntype?? '*any*)) |
| 18 |
; (node-closure (ntype?? '*any*))) |
| 19 |
; (sxpath1 '(equal? x)) -> (select-kids (node-equal? x)) |
| 20 |
; (sxpath1 '(eq? x)) -> (select-kids (node-eq? x)) |
| 21 |
; (sxpath1 '(or@ ...)) -> (select-kids (ntype-names?? |
| 22 |
; (cdr '(or@ ...)))) |
| 23 |
; (sxpath1 '(not@ ...)) -> (select-kids (sxml:invert |
| 24 |
; (ntype-names?? |
| 25 |
; (cdr '(not@ ...))))) |
| 26 |
; (sxpath1 '(ns-id:* x)) -> (select-kids |
| 27 |
; (ntype-namespace-id?? x)) |
| 28 |
; (sxpath1 ?symbol) -> (select-kids (ntype?? ?symbol)) |
| 29 |
; (sxpath1 ?string) -> (txpath ?string) |
| 30 |
; (sxpath1 procedure) -> procedure |
| 31 |
; (sxpath1 '(?symbol ...)) -> (sxpath1 '((?symbol) ...)) |
| 32 |
; (sxpath1 '(path reducer ...)) -> |
| 33 |
; (node-reduce (sxpath path) (sxpathr reducer) ...) |
| 34 |
; (sxpathr number) -> (node-pos number) |
| 35 |
; (sxpathr path-filter) -> (filter (sxpath path-filter)) |
| 36 |
(define (sxpath path . ns-binding) |
| 37 |
(let ((ns-binding (if (null? ns-binding) ns-binding (car ns-binding)))) |
| 38 |
(let loop ((converters '()) |
| 39 |
(root-vars '()) |
| 40 |
(path (if (string? path) (list path) path))) |
| 41 |
(cond |
| 42 |
((null? path) ; parsing is finished |
| 43 |
(lambda (node . root-var-binding) |
| 44 |
(let ((root-node |
| 45 |
(if (null? root-var-binding) node (car root-var-binding))) |
| 46 |
(var-binding |
| 47 |
(if |
| 48 |
(or (null? root-var-binding) (null? (cdr root-var-binding))) |
| 49 |
'() (cadr root-var-binding)))) |
| 50 |
(let rpt ((nodeset (if (nodeset? node) node (list node))) |
| 51 |
(conv (reverse converters)) |
| 52 |
(r-v (reverse root-vars))) |
| 53 |
(if |
| 54 |
(null? conv) ; the path is over |
| 55 |
nodeset |
| 56 |
(rpt |
| 57 |
(if (car r-v) ; the current converter consumes 3 arguments |
| 58 |
((car conv) nodeset root-node var-binding) |
| 59 |
((car conv) nodeset)) |
| 60 |
(cdr conv) |
| 61 |
(cdr r-v))))))) |
| 62 |
; or@ handler |
| 63 |
((and (pair? (car path)) |
| 64 |
(not (null? (car path))) |
| 65 |
(eq? 'or@ (caar path))) |
| 66 |
(loop (cons (select-kids (ntype-names?? (cdar path))) converters) |
| 67 |
(cons #f root-vars) |
| 68 |
(cdr path))) |
| 69 |
; not@ handler |
| 70 |
((and (pair? (car path)) |
| 71 |
(not (null? (car path))) |
| 72 |
(eq? 'not@ (caar path))) |
| 73 |
(loop (cons |
| 74 |
(select-kids (sxml:invert (ntype-names?? (cdar path)))) |
| 75 |
converters) |
| 76 |
(cons #f root-vars) |
| 77 |
(cdr path))) |
| 78 |
((procedure? (car path)) |
| 79 |
(loop (cons (car path) converters) |
| 80 |
(cons #t root-vars) |
| 81 |
(cdr path))) |
| 82 |
((eq? '// (car path)) |
| 83 |
(loop (cons (node-or |
| 84 |
(node-self (ntype?? '*any*)) |
| 85 |
(node-closure (ntype?? '*any*))) |
| 86 |
converters) |
| 87 |
(cons #f root-vars) |
| 88 |
(cdr path))) |
| 89 |
((symbol? (car path)) |
| 90 |
(loop (cons (select-kids (ntype?? (car path))) converters) |
| 91 |
(cons #f root-vars) |
| 92 |
(cdr path))) |
| 93 |
((string? (car path)) |
| 94 |
(and-let* |
| 95 |
((f (txpath (car path) ns-binding))) |
| 96 |
(loop (cons f converters) |
| 97 |
(cons #t root-vars) |
| 98 |
(cdr path)))) |
| 99 |
((and (pair? (car path)) (eq? 'equal? (caar path))) |
| 100 |
(loop (cons (select-kids (apply node-equal? (cdar path))) converters) |
| 101 |
(cons #f root-vars) |
| 102 |
(cdr path))) |
| 103 |
; ns-id:* handler |
| 104 |
((and (pair? (car path)) (eq? 'ns-id:* (caar path))) |
| 105 |
(loop |
| 106 |
(cons (select-kids (ntype-namespace-id?? (cadar path))) converters) |
| 107 |
(cons #f root-vars) |
| 108 |
(cdr path))) |
| 109 |
((and (pair? (car path)) (eq? 'eq? (caar path))) |
| 110 |
(loop (cons (select-kids (apply node-eq? (cdar path))) converters) |
| 111 |
(cons #f root-vars) |
| 112 |
(cdr path))) |
| 113 |
((pair? (car path)) |
| 114 |
(and-let* |
| 115 |
((select |
| 116 |
(if |
| 117 |
(symbol? (caar path)) |
| 118 |
(lambda (node root-node . var-binding) |
| 119 |
((select-kids (ntype?? (caar path))) node)) |
| 120 |
(sxpath (caar path))))) |
| 121 |
(let reducer ((reducing-path (cdar path)) |
| 122 |
(filters '())) |
| 123 |
(cond |
| 124 |
((null? reducing-path) |
| 125 |
(loop |
| 126 |
(cons |
| 127 |
(lambda (node root-node var-binding) |
| 128 |
(map-union |
| 129 |
(lambda (node) |
| 130 |
(let label ((nodeset (select node root-node var-binding)) |
| 131 |
(fs (reverse filters))) |
| 132 |
(if |
| 133 |
(null? fs) |
| 134 |
nodeset |
| 135 |
(label |
| 136 |
((car fs) nodeset root-node var-binding) |
| 137 |
(cdr fs))))) |
| 138 |
(if (nodeset? node) node (list node)))) |
| 139 |
converters) |
| 140 |
(cons #t root-vars) |
| 141 |
(cdr path))) |
| 142 |
((number? (car reducing-path)) |
| 143 |
(reducer |
| 144 |
(cdr reducing-path) |
| 145 |
(cons |
| 146 |
(lambda (node root-node var-binding) |
| 147 |
((node-pos (car reducing-path)) node)) |
| 148 |
filters))) |
| 149 |
(else |
| 150 |
(and-let* |
| 151 |
((func (sxpath (car reducing-path)))) |
| 152 |
(reducer |
| 153 |
(cdr reducing-path) |
| 154 |
(cons |
| 155 |
(lambda (node root-node var-binding) |
| 156 |
((sxml:filter |
| 157 |
(lambda (n) (func n root-node var-binding))) |
| 158 |
node)) |
| 159 |
filters)))))))) |
| 160 |
(else |
| 161 |
(cerr "Invalid path step: " (car path)) |
| 162 |
#f))))) |
| 163 |
|
| 164 |
|
| 165 |
;============================================================================== |
| 166 |
; Wrappers |
| 167 |
|
| 168 |
; sxpath always returns a list, which is #t in Scheme |
| 169 |
; if-sxpath returns #f instead of empty list |
| 170 |
(define (if-sxpath path) |
| 171 |
(lambda (obj) |
| 172 |
(let ((x ((sxpath path) obj))) |
| 173 |
(if (null? x) #f x)))) |
| 174 |
|
| 175 |
; Returns first node found, if any. |
| 176 |
; Otherwise returns #f. |
| 177 |
(define (if-car-sxpath path) |
| 178 |
(lambda (obj) |
| 179 |
(let ((x ((sxpath path) obj))) |
| 180 |
(if (null? x) #f (car x))))) |
| 181 |
|
| 182 |
; Returns first node found, if any. |
| 183 |
; Otherwise returns empty list. |
| 184 |
(define (car-sxpath path) |
| 185 |
(lambda (obj) |
| 186 |
(let ((x ((sxpath path) obj))) |
| 187 |
(if (null? x) '() (car x))))) |
| 188 |
|
| 189 |
;============================================================================== |
| 190 |
; lookup by a value of ID type attribute |
| 191 |
|
| 192 |
; Built an index as a list of (ID_value . element) pairs for given |
| 193 |
; node. lpaths are location paths for attributes of type ID. |
| 194 |
(define (sxml:id-alist node . lpaths) |
| 195 |
(apply |
| 196 |
append |
| 197 |
(map |
| 198 |
(lambda(lp) |
| 199 |
(let ((lpr (reverse lp))) |
| 200 |
(map |
| 201 |
(lambda (nd) |
| 202 |
(cons (sxml:attr nd (car lpr)) |
| 203 |
nd)) |
| 204 |
; Selects elements with ID attributes |
| 205 |
; using (lpath ,(node-self (sxpath '(@ attrname)))) |
| 206 |
((sxpath (reverse (cons |
| 207 |
(node-self (sxpath `(@ ,(car lpr)))) |
| 208 |
(cddr lpr)))) node)) |
| 209 |
)) |
| 210 |
lpaths))) |
| 211 |
|