1 ;;; -*- show-trailing-whitespace: t; indent-tabs-mode: nil -*-
3 ;;; Copyright (c) 2007,2008 David Lichteblau, Ivan Shvedunov.
4 ;;; All rights reserved.
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
10 ;;; * Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
13 ;;; * Redistributions in binary form must reproduce the above
14 ;;; copyright notice, this list of conditions and the following
15 ;;; disclaimer in the documentation and/or other materials
16 ;;; provided with the distribution.
18 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
19 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 (in-package :xuriella
)
33 (declaim (optimize (debug 2)))
38 (defparameter *available-instructions
* (make-hash-table :test
'equal
))
40 (defmacro define-instruction
(name (args-var env-var
) &body body
)
41 `(setf (get ',name
'xslt-instruction
)
42 (lambda (,args-var
,env-var
)
43 (declare (ignorable ,env-var
))
46 (define-instruction if
(args env
)
47 (destructuring-bind (test then
&optional else
) args
48 (let ((test-thunk (compile-xpath test env
))
49 (then-thunk (compile-instruction then env
))
50 (else-thunk (when else
(compile-instruction else env
))))
53 ((xpath:boolean-value
(funcall test-thunk ctx
))
54 (funcall then-thunk ctx
))
56 (funcall else-thunk ctx
)))))))
58 (define-instruction when
(args env
)
59 (destructuring-bind (test &rest body
) args
60 (compile-instruction `(if ,test
(progn ,@body
)) env
)))
62 (define-instruction unless
(args env
)
63 (destructuring-bind (test &rest body
) args
64 (compile-instruction `(if (:not
,test
) (progn ,@body
)) env
)))
66 (define-instruction cond
(args env
)
68 (destructuring-bind ((test &body body
) &rest clauses
) args
69 (compile-instruction (if (eq test t
)
77 (define-instruction progn
(args env
)
79 (let ((first-thunk (compile-instruction (first args
) env
))
80 (rest-thunk (compile-instruction `(progn ,@(rest args
)) env
)))
82 (funcall first-thunk ctx
)
83 (funcall rest-thunk ctx
)))
86 (defun decode-qname/runtime
(qname namespaces attributep
)
88 (multiple-value-bind (prefix local-name
)
91 (if (or prefix
(not attributep
))
92 (cdr (assoc prefix namespaces
:test
'equal
))
95 (cxml:well-formedness-violation
()
96 (xslt-error "not a qname: ~A" qname
))))
98 (define-instruction xsl
:element
(args env
)
99 (destructuring-bind ((name &key namespace use-attribute-sets
)
102 (declare (ignore use-attribute-sets
)) ;fixme
103 (multiple-value-bind (name-thunk constant-name-p
)
104 (compile-avt name env
)
105 (multiple-value-bind (ns-thunk constant-ns-p
)
107 (compile-avt namespace env
)
109 (let ((body-thunk (compile-instruction `(progn ,@body
) env
)))
110 (if (and constant-name-p constant-ns-p
)
111 (compile-element/constant-name name namespace env body-thunk
)
112 (compile-element/runtime name-thunk ns-thunk body-thunk
)))))))
114 (defun compile-element/constant-name
(qname namespace env body-thunk
)
115 ;; the simple case: compile-time decoding of the QName
116 (multiple-value-bind (local-name uri prefix
)
117 (decode-qname qname env nil
)
119 (setf uri namespace
))
123 (with-element (local-name uri
:suggested-prefix prefix
)
124 (funcall body-thunk ctx
)))
126 ;; ERROR rather than CERROR because saxon doesn't do the recovery,
127 ;; and the official output illustrates recovery but is useless as
129 (xslt-error "namespace not found: ~A" prefix
)
131 (let ((*start-tag-written-p
* t
))
132 (declare (special *start-tag-written-p
*))
133 (funcall body-thunk ctx
)))))))
135 (defun compile-element/runtime
(name-thunk ns-thunk body-thunk
)
136 ;; run-time decoding of the QName, but using the same namespaces
137 ;; that would have been known at compilation time.
138 (let ((namespaces *namespaces
*))
140 (let ((qname (funcall name-thunk ctx
)))
141 (multiple-value-bind (local-name uri prefix
)
142 (decode-qname/runtime qname namespaces nil
)
144 (setf uri
(funcall ns-thunk ctx
)))
147 (with-element (local-name uri
:suggested-prefix prefix
)
148 (funcall body-thunk ctx
)))))))
150 (define-instruction xsl
:use-attribute-sets
(args env
)
151 (destructuring-bind (str) args
152 (let ((sets (mapcar (lambda (qname)
153 (multiple-value-list (decode-qname qname env nil
)))
156 (loop for
(local-name uri nil
) in sets do
157 (dolist (thunk (find-attribute-set local-name uri
))
158 (funcall thunk ctx
)))))))
160 (define-instruction xsl
:attribute
(args env
)
161 (destructuring-bind ((name &key namespace
) &body body
) args
163 (xslt-error "xsl:attribute: name not specified"))
164 (multiple-value-bind (name-thunk constant-name-p
)
165 (compile-avt name env
)
166 (multiple-value-bind (ns-thunk constant-ns-p
)
168 (compile-avt namespace env
)
170 (let ((value-thunk (compile-instruction `(progn ,@body
) env
)))
171 (if (and constant-name-p constant-ns-p
)
172 (compile-attribute/constant-name name namespace env value-thunk
)
173 (compile-attribute/runtime name-thunk ns-thunk value-thunk
)))))))
175 (defun compile-attribute/constant-name
(qname namespace env value-thunk
)
176 ;; the simple case: compile-time decoding of the QName
177 (multiple-value-bind (local-name uri prefix
)
178 (decode-qname qname env t
)
180 (setf uri namespace
))
182 (write-attribute local-name
184 (with-toplevel-text-output-sink (s)
186 (funcall value-thunk ctx
)))
187 :suggested-prefix prefix
))))
189 (defun compile-attribute/runtime
(name-thunk ns-thunk value-thunk
)
190 ;; run-time decoding of the QName, but using the same namespaces
191 ;; that would have been known at compilation time.
192 (let ((namespaces *namespaces
*))
194 (let ((qname (funcall name-thunk ctx
)))
195 (multiple-value-bind (local-name uri prefix
)
196 (decode-qname/runtime qname namespaces t
)
198 (setf uri
(funcall ns-thunk ctx
)))
199 (write-attribute local-name
201 (with-toplevel-text-output-sink (s)
203 (funcall value-thunk ctx
)))
204 :suggested-prefix prefix
))))))
206 ;; zzz Also elides (later) namespaces hidden by (earlier) ones.
207 ;; zzz Reverses order.
208 (defun remove-excluded-namespaces
209 (namespaces &optional
(excluded-uris *excluded-namespaces
*))
210 (let ((koerbchen '())
213 for cons in namespaces
214 for
(prefix* . uri
) = cons
215 for prefix
= (or prefix
* "")
218 ((find prefix kroepfchen
:test
#'equal
))
219 ((find prefix koerbchen
:test
#'equal
:key
#'car
))
220 ((find uri excluded-uris
:test
#'equal
)
221 (push prefix kroepfchen
))
223 (push cons koerbchen
))))
226 (define-instruction xsl
:literal-element
(args env
)
228 ((local-name &optional
(uri "") suggested-prefix
) &body body
)
230 (let ((body-thunk (compile-instruction `(progn ,@body
) env
))
231 (namespaces (remove-excluded-namespaces *namespaces
*)))
233 (with-element (local-name (or uri
"")
234 :suggested-prefix suggested-prefix
235 :extra-namespaces namespaces
237 (funcall body-thunk ctx
))))))
239 (define-instruction xsl
:literal-attribute
(args env
)
240 (destructuring-bind ((local-name &optional uri suggested-prefix
) value
) args
241 (let ((value-thunk (compile-avt value env
)))
243 (write-attribute local-name
245 (funcall value-thunk ctx
)
247 :suggested-prefix suggested-prefix
)))))
249 (define-instruction xsl
:text
(args env
)
250 (destructuring-bind (str) args
252 (declare (ignore ctx
))
255 (define-instruction xsl
:unescaped-text
(args env
)
256 (destructuring-bind (str) args
258 (declare (ignore ctx
))
259 (write-unescaped str
))))
261 (define-instruction xsl
:processing-instruction
(args env
)
262 (destructuring-bind (name &rest body
) args
263 (let ((name-thunk (compile-avt name env
))
264 (value-thunk (compile-instruction `(progn ,@body
) env
)))
266 (write-processing-instruction
267 (funcall name-thunk ctx
)
268 (with-toplevel-text-output-sink (s)
270 (funcall value-thunk ctx
))))))))
272 (define-instruction xsl
:comment
(args env
)
273 (let ((value-thunk (compile-instruction `(progn ,@args
) env
)))
275 (write-comment (with-toplevel-text-output-sink (s)
277 (funcall value-thunk ctx
)))))))
279 (define-instruction xsl
:value-of
(args env
)
280 (destructuring-bind (xpath) args
281 (let ((thunk (compile-xpath xpath env
)))
284 (write-text (xpath:string-value
(funcall thunk ctx
))))
285 "value-of ~s = ~s" xpath
:result
))))
287 (define-instruction xsl
:unescaped-value-of
(args env
)
288 (destructuring-bind (xpath) args
289 (let ((thunk (compile-xpath xpath env
)))
291 (write-unescaped (xpath:string-value
(funcall thunk ctx
)))))))
293 (define-instruction xsl
:copy-of
(args env
)
294 (destructuring-bind (xpath) args
295 (let ((thunk (compile-xpath xpath env
))
296 ;; FIXME: what was this for? --david
297 #+(or) (v (intern-variable "varName" "")))
300 (let ((result (funcall thunk ctx
)))
302 (xpath:node-set
;; FIXME: variables can contain node sets w/fragments inside. Maybe just fragments would do?
303 (xpath:map-node-set
#'copy-into-result
(xpath:sort-node-set result
)))
304 (result-tree-fragment
305 (copy-into-result result
))
307 (write-text (xpath:string-value result
))))))
308 "copy-of ~s" xpath
))))
310 (defun copy-into-result (node)
312 ((result-tree-fragment-p node
)
313 (stp:do-children
(child (result-tree-fragment-node node
))
314 (copy-into-result child
)))
315 ((xpath-protocol:node-type-p node
:element
)
316 (with-element ((xpath-protocol:local-name node
)
317 (xpath-protocol:namespace-uri node
)
318 :suggested-prefix
(xpath-protocol:namespace-prefix node
)
319 :extra-namespaces
(namespaces-as-alist node
))
320 (map-pipe-eagerly #'copy-into-result
321 (xpath-protocol:attribute-pipe node
))
322 (map-pipe-eagerly #'copy-into-result
323 (xpath-protocol:child-pipe node
))))
324 ((xpath-protocol:node-type-p node
:document
)
325 (map-pipe-eagerly #'copy-into-result
326 (xpath-protocol:child-pipe node
)))
328 (copy-leaf-node node
))))
330 (defparameter *lower-first-order
*
331 #(#\
#\
! #\" #\
# #\$
#\%
#\
& #\' #\
( #\
) #\
* #\
+ #\
, #\-
#\.
#\
/ #\
0 #\
1 #\
2
332 #\
3 #\
4 #\
5 #\
6 #\
7 #\
8 #\
9 #\
: #\
; #\< #\= #\> #\? #\@ #\H #\J #\L #\N #\P
333 #\R
#\T
#\V
#\X
#\Z
#\\ #\^
#\
` #\b #\d
#\f #\h
#\j
#\l
#\n #\p
#\r #\t #\v
334 #\x
#\z
#\A
#\B
#\C
#\D
#\E
#\F
#\G
#\I
#\K
#\M
#\O
#\Q
#\S
#\U
#\W
#\Y
#\
[
335 #\
] #\_
#\a #\c
#\e
#\g
#\i
#\k
#\m
#\o
#\q
#\s
#\u
#\w
#\y
#\
{ #\|
#\
} #\~
338 (defparameter *upper-first-order
*
339 #(#\
#\
! #\" #\
# #\$
#\%
#\
& #\' #\
( #\
) #\
* #\
+ #\
, #\-
#\.
#\
/ #\
0 #\
1 #\
2
340 #\
3 #\
4 #\
5 #\
6 #\
7 #\
8 #\
9 #\
: #\
; #\< #\= #\> #\? #\@ #\G #\I #\K #\M #\O
341 #\Q
#\S
#\U
#\W
#\Y
#\
[ #\
] #\_
#\a #\c
#\e
#\g
#\i
#\k
#\m
#\o
#\q
#\s
#\u
342 #\w
#\y
#\A
#\B
#\C
#\D
#\E
#\F
#\H
#\J
#\L
#\N
#\P
#\R
#\T
#\V
#\X
#\Z
#\\
343 #\^
#\
` #\b #\d
#\f #\h
#\j
#\l
#\n #\p
#\r #\t #\v #\x
#\z
#\
{ #\|
#\
} #\~
346 (defun make-collation-key (str table
)
349 (let ((code (char-code char
)))
351 (elt table
(- code
32))
355 (defun make-sorter (spec env
)
356 (destructuring-bind (&key select lang data-type order case-order
)
358 (declare (ignore lang
))
359 (let ((select-thunk (compile-xpath (or select
".") env
))
360 (numberp (equal data-type
"number"))
361 (f (if (equal order
"descending") -
1 1))
362 (char-table (if (equal case-order
"lower-first")
364 *upper-first-order
*)))
366 (let ((i (xpath:string-value
367 (funcall select-thunk
(xpath:make-context a
))))
368 (j (xpath:string-value
369 (funcall select-thunk
(xpath:make-context b
)))))
372 (let ((n-a (xpath:number-value i
))
373 (n-b (xpath:number-value j
)))
374 (cond ((and (xpath::nan-p n-a
)
375 (not (xpath::nan-p n-b
)))
377 ((and (not (xpath::nan-p n-a
))
380 ((xpath::compare-numbers
'< n-a n-b
) -
1)
381 ((xpath::compare-numbers
'> n-a n-b
) 1)
383 ;; zzz Unicode support!
384 (let ((k (make-collation-key i char-table
))
385 (l (make-collation-key j char-table
)))
391 (defun compose-sorters (sorters)
393 (let ((this (car sorters
))
394 (next (compose-sorters (rest sorters
))))
396 (let ((d (funcall this a b
)))
402 (defun make-sort-predicate (decls env
)
405 (mapcar (lambda (x) (make-sorter x env
)) decls
))))
407 (minusp (funcall sorter a b
)))))
409 (define-instruction xsl
:for-each
(args env
)
410 (destructuring-bind (select &optional decls
&rest body
) args
411 (unless (and (consp decls
)
412 (eq (car decls
) 'declare
))
415 (let ((select-thunk (compile-xpath select env
))
416 (body-thunk (compile-instruction `(progn ,@body
) env
))
419 (make-sort-predicate (cdr decls
) env
))))
421 (let ((selected (funcall select-thunk ctx
)))
422 (unless (xpath:node-set-p selected
)
423 (xslt-error "for-each select expression should yield a node-set"))
424 (let ((nodes (xpath::force
425 (xpath::sorted-pipe-of selected
))))
427 (setf nodes
(stable-sort (copy-list nodes
) sort-predicate
)))
429 with n
= (length nodes
)
434 (xpath:make-context node
(lambda () n
) i
)))))))))
436 (define-instruction xsl
:with-namespaces
(args env
)
437 (destructuring-bind ((&rest forms
) &rest body
) args
438 (let ((*namespaces
* *namespaces
*))
440 (destructuring-bind (prefix uri
) form
441 (push (cons prefix uri
) *namespaces
*)))
442 (compile-instruction `(progn ,@body
) env
))))
444 (define-instruction xsl
:with-excluded-namespaces
(args env
)
445 (destructuring-bind ((&rest uris
) &rest body
) args
446 (let ((*excluded-namespaces
* (append uris
*excluded-namespaces
*)))
447 (compile-instruction `(progn ,@body
) env
))))
449 (define-instruction xsl
:with-extension-namespaces
(args env
)
450 (destructuring-bind ((&rest uris
) &rest body
) args
451 (let ((*extension-namespaces
* (append uris
*extension-namespaces
*)))
452 (compile-instruction `(progn ,@body
) env
))))
454 ;; XSLT disallows multiple definitions of the same variable within a
455 ;; template. Local variables can shadow global variables though.
456 ;; Since our LET syntax makes it natural to shadow local variables the
457 ;; Lisp way, we check for duplicate variables only where instructed to
458 ;; by the XML syntax parser using WITH-DUPLICATES-CHECK:
459 (defvar *template-variables
* nil
)
461 (define-instruction xsl
:with-duplicates-check
(args env
)
462 (let ((*template-variables
* *template-variables
*))
463 (destructuring-bind ((&rest qnames
) &rest body
) args
464 (dolist (qname qnames
)
465 (multiple-value-bind (local-name uri
)
466 (decode-qname qname env nil
)
467 (let ((key (cons local-name uri
)))
468 (when (find key
*template-variables
* :test
#'equal
)
469 (xslt-error "duplicate variable: ~A, ~A" local-name uri
))
470 (push key
*template-variables
*))))
471 (compile-instruction `(progn ,@body
) env
))))
473 (define-instruction xsl
:with-base-uri
(args env
)
474 (destructuring-bind (uri &rest body
) args
475 (let ((*instruction-base-uri
* uri
))
476 (compile-instruction `(progn ,@body
) env
))))
478 (defstruct (result-tree-fragment
479 (:constructor make-result-tree-fragment
(node)))
482 (define-default-method xpath-protocol
:node-p
483 ((node result-tree-fragment
))
486 (define-default-method xpath-protocol
:node-text
487 ((node result-tree-fragment
))
488 (xpath-protocol:node-text
(result-tree-fragment-node node
)))
490 (defun apply-to-result-tree-fragment (ctx thunk
)
492 (with-xml-output (make-stpx-builder)
493 (with-element ("fragment" "")
494 (funcall thunk ctx
)))))
495 (make-result-tree-fragment (stp:document-element document
))))
497 (define-instruction let
(args env
)
498 (destructuring-bind ((&rest forms
) &rest body
) args
499 (let* ((old-top (length *lexical-variable-declarations
*))
500 (vars-and-names (compile-var-bindings/nointern forms env
))
502 (loop for
((local-name . uri
) thunk
) in vars-and-names
504 (list (push-variable local-name
506 *lexical-variable-declarations
*)
508 (let ((thunk (compile-instruction `(progn ,@body
) env
)))
509 (fill *lexical-variable-declarations
* nil
:start old-top
)
511 (loop for
(index var-thunk
) in vars-and-positions
512 do
(setf (lexical-variable-value index
)
513 (funcall var-thunk ctx
)))
514 (funcall thunk ctx
))))))
516 (define-instruction let
* (args env
)
517 (destructuring-bind ((&rest forms
) &rest body
) args
519 (compile-instruction `(let (,(car forms
))
520 (let* (,@(cdr forms
))
523 (compile-instruction `(progn ,@body
) env
))))
525 (define-instruction xsl
:message
(args env
)
526 (compile-message #'warn args env
))
528 (define-instruction xsl
:terminate
(args env
)
529 (compile-message #'error args env
))
531 (defun namespaces-as-alist (element)
532 (let ((namespaces '()))
533 (do-pipe (ns (xpath-protocol:namespace-pipe element
))
534 (push (cons (xpath-protocol:local-name ns
)
535 (xpath-protocol:node-text ns
))
539 (define-instruction xsl
:copy
(args env
)
540 (let ((body (compile-instruction `(progn ,@args
) env
)))
542 (let ((node (xpath:context-node ctx
)))
544 ((xpath-protocol:node-type-p node
:element
)
546 ((xpath-protocol:local-name node
)
547 (xpath-protocol:namespace-uri node
)
548 :suggested-prefix
(xpath-protocol:namespace-prefix node
)
549 :extra-namespaces
(namespaces-as-alist node
))
551 ((xpath-protocol:node-type-p node
:document
)
554 (copy-leaf-node node
)))))))
556 (defun copy-leaf-node (node)
558 ((xpath-protocol:node-type-p node
:text
)
559 (etypecase (if (typep node
'stripping-node
)
560 (stripping-node-target node
)
562 (unescaped-text (write-unescaped (xpath-protocol:node-text node
)))
563 (stp:text
(write-text (xpath-protocol:node-text node
)))))
564 ((xpath-protocol:node-type-p node
:comment
)
565 (write-comment (xpath-protocol:node-text node
)))
566 ((xpath-protocol:node-type-p node
:processing-instruction
)
567 (write-processing-instruction
568 (xpath-protocol:processing-instruction-target node
)
569 (xpath-protocol:node-text node
)))
570 ((xpath-protocol:node-type-p node
:attribute
)
572 (xpath-protocol:local-name node
)
573 (xpath-protocol:namespace-uri node
)
574 (xpath-protocol:node-text node
)
575 :suggested-prefix
(xpath-protocol:namespace-prefix node
)))
576 ((xpath-protocol:node-type-p node
:namespace
)
577 (write-extra-namespace
578 (xpath-protocol:local-name node
)
579 (xpath-protocol:node-text node
)
582 (error "don't know how to copy node ~A" node
))))
584 (defun compile-message (fn args env
)
585 (let ((thunk (compile-instruction `(progn ,@args
) env
)))
588 (with-xml-output (cxml:make-string-sink
)
589 (funcall thunk ctx
))))))
591 (define-instruction xsl
:apply-templates
(args env
)
592 (destructuring-bind ((&key select mode
) &rest param-binding-specs
) args
594 (when (and (consp (car param-binding-specs
))
595 (eq (caar param-binding-specs
) 'declare
))
596 (cdr (pop param-binding-specs
))))
598 (compile-xpath (or select
"child::node()") env
))
600 (compile-var-bindings param-binding-specs env
))
603 (make-sort-predicate decls env
))))
604 (multiple-value-bind (mode-local-name mode-uri
)
605 (and mode
(decode-qname mode env nil
))
607 (apply-templates/list
609 (xpath::sorted-pipe-of
(funcall select-thunk ctx
)))
611 (loop for
(name nil value-thunk
) in param-bindings
612 collect
(list name
(funcall value-thunk ctx
)))
613 :sort-predicate sort-predicate
615 (or (find-mode *stylesheet
*
620 (define-instruction xsl
:apply-imports
(args env
)
621 (declare (ignore args env
))
623 (declare (ignore ctx
))
624 (funcall *apply-imports
*)))
626 (define-instruction xsl
:call-template
(args env
)
627 (destructuring-bind (name &rest param-binding-specs
) args
628 (let ((param-bindings
629 (compile-var-bindings param-binding-specs env
)))
630 (multiple-value-bind (local-name uri
)
631 (decode-qname name env nil
)
632 (setf name
(cons local-name uri
)))
634 (call-template ctx name
635 (loop for
(name nil value-thunk
) in param-bindings
636 collect
(list name
(funcall value-thunk ctx
))))))))
638 ;; fixme: incompatible with XSLT 2.0
639 (define-instruction xsl
:document
(args env
)
640 (destructuring-bind ((href &key method indent doctype-public doctype-system
)
643 (declare (ignore doctype-public doctype-system
)) ;fixme
644 (let ((thunk (compile-instruction `(progn ,@body
) env
))
645 (href-thunk (compile-avt href env
)))
649 (puri:merge-uris
(funcall href-thunk ctx
)
650 (xpath-protocol:base-uri
651 (xpath:context-node ctx
))))))
652 (ensure-directories-exist pathname
) ;really?
653 (invoke-with-output-sink
656 (make-output-specification :method
(or method
"XML") :indent indent
)
659 (defun compile-instruction (form env
)
661 (funcall (or (get (car form
) 'xslt-instruction
)
662 (error "undefined instruction: ~A" (car form
)))
665 "instruction ~s" (car form
)))
667 ;;: WTF: "A right curly brace inside a Literal in an expression is not
668 ;;; recognized as terminating the expression."
670 ;;; Da hilft nur tagbody.
671 (defun parse-attribute-value-template (template-string)
672 (with-input-from-string (input template-string
)
673 (let ((ordinary (make-string-output-stream))
674 (xpath (make-string-output-stream))
676 (c (read-char input nil
:eof
)))
678 (let ((o (get-output-stream-string ordinary
)))
679 (when (plusp (length o
))
680 (push (list :data o
) tokens
)))
681 (let ((x (get-output-stream-string xpath
)))
682 (when (plusp (length x
))
683 (push (list :xpath x
) tokens
))))
685 (write-char c ordinary
))
687 (write-char c xpath
)))
688 (macrolet ((goto (target)
690 (setf c
(read-char input nil
:eof
))
711 (goto in-single-quote
))
713 (xslt-error "unexpected end of avt")))
722 (goto in-single-quote
))
725 (goto in-double-quote
))
727 (goto seen-closing-
}))
729 (xslt-error "unexpected end of avt")))
739 (xslt-error "unexpected end of avt")))
741 (goto in-single-quote
)
749 (xslt-error "unexpected end of avt")))
751 (goto in-double-quote
)
772 (xslt-error "unexpected closing brace in avt")
778 (defun compile-avt (template-string env
)
784 (constantly (second x
)))
787 (compile-xpath (second x
) env
))))
788 (parse-attribute-value-template template-string
))))
789 (values (lambda (ctx)
790 (with-output-to-string (s)
792 (write-string (xpath:string-value
(funcall fn ctx
)) s
))))
796 ;;;; Indentation for slime
798 (defmacro define-indentation
(name (&rest args
))
799 (labels ((collect-variables (list)
805 (collect-variables sub
))
807 (if (eql (mismatch "&" (symbol-name sub
)) 1)
810 `(defmacro ,name
(,@args
)
811 (declare (ignorable ,@(collect-variables args
)))
812 (error "XSL indentation helper ~A used literally in lisp code"
815 (define-indentation xsl
:element
816 ((name &key namespace use-attribute-sets
) &body body
))
817 (define-indentation xsl
:literal-element
((name &optional uri
) &body body
))
818 (define-indentation xsl
:attribute
((name &key namespace
) &body body
))
819 (define-indentation xsl
:literal-attribute
((name &optional uri
) &body body
))
820 (define-indentation xsl
:text
(str))
821 (define-indentation xsl
:processing-instruction
(name &body body
))
822 (define-indentation xsl
:comment
(&body body
))
823 (define-indentation xsl
:value-of
(xpath))
824 (define-indentation xsl
:unescaped-value-of
(xpath))
825 (define-indentation xsl
:for-each
(select &body decls-and-body
))
826 (define-indentation xsl
:message
(&body body
))
827 (define-indentation xsl
:terminate
(&body body
))
828 (define-indentation xsl
:apply-templates
((&key select mode
) &body decls-and-body
))
829 (define-indentation xsl
:call-template
(name &rest parameters
))
830 (define-indentation xsl
:copy-of
(xpath))
834 (defun test-instruction (form document
)
835 (let ((thunk (compile-instruction form
(make-instance 'lexical-environment
)))
836 (root (cxml:parse document
(stp:make-builder
))))
837 (with-xml-output (cxml:make-string-sink
)
838 (funcall thunk
(xpath:make-context root
)))))