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)))
36 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
37 (defvar *xsl
* "http://www.w3.org/1999/XSL/Transform")
38 (defvar *xml
* "http://www.w3.org/XML/1998/namespace")
39 (defvar *html
* "http://www.w3.org/1999/xhtml"))
44 (define-condition xslt-error
(simple-error)
46 (:documentation
"The class of all XSLT errors."))
48 (define-condition recoverable-xslt-error
(xslt-error)
50 (:documentation
"The class of recoverable XSLT errors."))
52 (defun xslt-error (fmt &rest args
)
53 (error 'xslt-error
:format-control fmt
:format-arguments args
))
55 (defun xslt-cerror (fmt &rest args
)
56 (with-simple-restart (recover "recover")
57 (error 'recoverable-xslt-error
59 :format-arguments args
)))
63 (defmacro handler-case
* (form &rest clauses
)
64 ;; like HANDLER-CASE if *DEBUG* is off. If it's on, don't establish
65 ;; a handler at all so that we see the real stack traces. (We could use
66 ;; HANDLER-BIND here and check at signalling time, but doesn't seem
68 (let ((doit (gensym)))
69 `(flet ((,doit
() ,form
))
76 (defun compile-xpath (xpath &optional env
)
78 (xpath:compile-xpath xpath env
)
79 (xpath:xpath-error
(c)
80 (xslt-error "~A" c
))))
82 (defmacro with-stack-limit
((&optional
) &body body
)
83 `(invoke-with-stack-limit (lambda () ,@body
)))
86 ;;;; Helper functions and macros
88 (defun check-for-invalid-attributes (valid-names node
)
89 (labels ((check-attribute (a)
91 (let ((uri (stp:namespace-uri a
)))
92 (or (and (plusp (length uri
)) (not (equal uri
*xsl
*)))
93 (find (cons (stp:local-name a
) uri
)
96 (xslt-error "attribute ~A not allowed on ~A"
98 (stp:local-name node
)))))
99 (stp:map-attributes nil
#'check-attribute node
)))
101 (defmacro only-with-attributes
((&rest specs
) node
&body body
)
103 (mapcar (lambda (entry)
104 (if (and (listp entry
) (cdr entry
))
105 (destructuring-bind (name &optional
(uri ""))
108 (cons (string-downcase
110 (symbol-name entry
)))
114 `(let ((,%NODE
,node
))
115 (declare (ignorable ,%NODE
))
116 (check-for-invalid-attributes ',valid-names
,%NODE
)
117 (stp:with-attributes
,specs
,%NODE
120 (defun map-pipe-eagerly (fn pipe
)
121 (xpath::enumerate pipe
:key fn
:result nil
))
123 (defmacro do-pipe
((var pipe
&optional result
) &body body
)
125 (map-pipe-eagerly #'(lambda (,var
) ,@body
) ,pipe
)
129 ;;;; XSLT-ENVIRONMENT and XSLT-CONTEXT
131 (defparameter *namespaces
*
133 ("xmlns" .
#"http://www.w3.org/2000/xmlns/")
134 ("xml" .
#"http://www.w3.org/XML/1998/namespace")))
136 (defvar *global-variable-declarations
*)
137 (defvar *lexical-variable-declarations
*)
139 (defvar *global-variable-values
*)
140 (defvar *lexical-variable-values
*)
142 (defclass xslt-environment
() ())
144 (defun split-qname (str)
146 (multiple-value-bind (prefix local-name
)
147 (cxml::split-qname str
)
149 ;; FIXME: cxml should really offer a function that does
150 ;; checks for NCName and QName in a sensible way for user code.
151 ;; cxml::split-qname is tailored to the needs of the parser.
153 ;; For now, let's just check the syntax explicitly.
154 (and (or (null prefix
) (xpath::nc-name-p prefix
))
155 (xpath::nc-name-p local-name
))
156 (xslt-error "not a qname: ~A" str
))
157 (values prefix local-name
))
158 (cxml:well-formedness-violation
()
159 (xslt-error "not a qname: ~A" str
))))
161 (defun decode-qname (qname env attributep
)
162 (multiple-value-bind (prefix local-name
)
165 (if (or prefix
(not attributep
))
166 (xpath-sys:environment-find-namespace env
(or prefix
""))
170 (defmethod xpath-sys:environment-find-namespace
((env xslt-environment
) prefix
)
171 (or (cdr (assoc prefix
*namespaces
* :test
'equal
))
173 ;; Change the entire code base to represent "no prefix" as the
174 ;; empty string consistently. unparse.lisp has already been changed.
175 (and (equal prefix
"")
176 (cdr (assoc nil
*namespaces
* :test
'equal
)))
177 (and (eql prefix nil
)
178 (cdr (assoc "" *namespaces
* :test
'equal
)))))
180 (defun find-variable-index (local-name uri table
)
181 (position (cons local-name uri
) table
:test
'equal
))
183 (defun intern-global-variable (local-name uri
)
184 (or (find-variable-index local-name uri
*global-variable-declarations
*)
185 (push-variable local-name uri
*global-variable-declarations
*)))
187 (defun push-variable (local-name uri table
)
190 (vector-push-extend (cons local-name uri
) table
)))
192 (defun lexical-variable-value (index &optional
(errorp t
))
193 (let ((result (svref *lexical-variable-values
* index
)))
195 (assert (not (eq result
'unbound
))))
198 (defun (setf lexical-variable-value
) (newval index
)
199 (assert (not (eq newval
'unbound
)))
200 (setf (svref *lexical-variable-values
* index
) newval
))
202 (defun global-variable-value (index &optional
(errorp t
))
203 (let ((result (svref *global-variable-values
* index
)))
205 (assert (not (eq result
'unbound
))))
208 (defun (setf global-variable-value
) (newval index
)
209 (assert (not (eq newval
'unbound
)))
210 (setf (svref *global-variable-values
* index
) newval
))
212 (defmethod xpath-sys:environment-find-function
213 ((env xslt-environment
) lname uri
)
215 (or (xpath-sys:find-xpath-function lname
*xsl
*)
216 (xpath-sys:find-xpath-function lname uri
))
217 (xpath-sys:find-xpath-function lname uri
)))
219 (defmethod xpath-sys:environment-find-variable
220 ((env xslt-environment
) lname uri
)
222 (find-variable-index lname uri
*lexical-variable-declarations
*)))
225 (declare (ignore ctx
))
226 (svref *lexical-variable-values
* index
)))))
228 (defclass lexical-xslt-environment
(xslt-environment) ())
230 (defmethod xpath-sys:environment-find-variable
231 ((env lexical-xslt-environment
) lname uri
)
232 (or (call-next-method)
234 (find-variable-index lname uri
*global-variable-declarations
*)))
238 (declare (ignore ctx
))
239 (svref *global-variable-values
* index
))
240 "global ~s (uri ~s) = ~s" lname uri
:result
)))))
242 (defclass global-variable-environment
(xslt-environment)
243 ((initial-global-variable-thunks
244 :initarg
:initial-global-variable-thunks
245 :accessor initial-global-variable-thunks
)))
247 (defmethod xpath-sys:environment-find-variable
248 ((env global-variable-environment
) lname uri
)
249 (or (call-next-method)
250 (gethash (cons lname uri
) (initial-global-variable-thunks env
))))
253 ;;;; TOPLEVEL-TEXT-OUTPUT-SINK
255 ;;;; A sink that serializes only text not contained in any element.
257 (defmacro with-toplevel-text-output-sink
((var) &body body
)
258 `(invoke-with-toplevel-text-output-sink (lambda (,var
) ,@body
)))
260 (defclass toplevel-text-output-sink
(sax:default-handler
)
261 ((target :initarg
:target
:accessor text-output-sink-target
)
262 (depth :initform
0 :accessor textoutput-sink-depth
)))
264 (defmethod sax:start-element
((sink toplevel-text-output-sink
)
265 namespace-uri local-name qname attributes
)
266 (declare (ignore namespace-uri local-name qname attributes
))
267 (incf (textoutput-sink-depth sink
)))
269 (defmethod sax:characters
((sink toplevel-text-output-sink
) data
)
270 (when (zerop (textoutput-sink-depth sink
))
271 (write-string data
(text-output-sink-target sink
))))
273 (defmethod sax:unescaped
((sink toplevel-text-output-sink
) data
)
274 (sax:characters sink data
))
276 (defmethod sax:end-element
((sink toplevel-text-output-sink
)
277 namespace-uri local-name qname
)
278 (declare (ignore namespace-uri local-name qname
))
279 (decf (textoutput-sink-depth sink
)))
281 (defun invoke-with-toplevel-text-output-sink (fn)
282 (with-output-to-string (s)
283 (funcall fn
(make-instance 'toplevel-text-output-sink
:target s
))))
288 ;;;; A sink that passes through only text (at any level) and turns to
289 ;;;; into unescaped characters.
291 (defclass text-filter
(sax:default-handler
)
292 ((target :initarg
:target
:accessor text-filter-target
)))
294 (defmethod sax:characters
((sink text-filter
) data
)
295 (sax:unescaped
(text-filter-target sink
) data
))
297 (defmethod sax:unescaped
((sink text-filter
) data
)
298 (sax:unescaped
(text-filter-target sink
) data
))
300 (defmethod sax:end-document
((sink text-filter
))
301 (sax:end-document
(text-filter-target sink
)))
303 (defun make-text-filter (target)
304 (make-instance 'text-filter
:target target
))
309 ;;;; A sink that recovers from sax:unescaped using sax:characters, as per
312 (defclass escaper
(cxml:broadcast-handler
)
315 (defmethod sax:unescaped
((sink escaper
) data
)
316 (sax:characters sink data
))
318 (defun make-escaper (target)
319 (make-instance 'escaper
:handlers
(list target
)))
324 (defun of-name (local-name)
325 (stp:of-name local-name
*xsl
*))
327 (defun namep (node local-name
)
328 (and (typep node
'(or stp
:element stp
:attribute
))
329 (equal (stp:namespace-uri node
) *xsl
*)
330 (equal (stp:local-name node
) local-name
)))
333 ;;;; PARSE-STYLESHEET
335 (defstruct stylesheet
336 (modes (make-hash-table :test
'equal
))
337 (global-variables (make-empty-declaration-array))
338 (output-specification (make-output-specification))
340 (named-templates (make-hash-table :test
'equal
))
341 (attribute-sets (make-hash-table :test
'equal
))
342 (keys (make-hash-table :test
'equal
))
343 (namespace-aliases (make-hash-table :test
'equal
))
344 (decimal-formats (make-hash-table :test
'equal
)))
346 (defstruct mode
(templates nil
))
348 (defun find-mode (stylesheet local-name
&optional uri
)
349 (gethash (cons local-name uri
) (stylesheet-modes stylesheet
)))
351 (defun ensure-mode (stylesheet &optional local-name uri
)
352 (or (find-mode stylesheet local-name uri
)
353 (setf (gethash (cons local-name uri
) (stylesheet-modes stylesheet
))
356 (defun ensure-mode/qname
(stylesheet qname env
)
358 (multiple-value-bind (local-name uri
)
359 (decode-qname qname env nil
)
360 (ensure-mode stylesheet local-name uri
))
361 (find-mode stylesheet nil
)))
363 (defun acons-namespaces (element &optional
(bindings *namespaces
*))
364 (map-namespace-declarations (lambda (prefix uri
)
365 (push (cons prefix uri
) bindings
))
369 (defun find-key (name stylesheet
)
370 (or (gethash name
(stylesheet-keys stylesheet
))
371 (xslt-error "unknown key: ~a" name
)))
373 (defun make-key (match use
) (cons match use
))
375 (defun key-match (key) (car key
))
377 (defun key-use (key) (cdr key
))
379 (defun add-key (stylesheet name match use
)
380 (if (gethash name
(stylesheet-keys stylesheet
))
381 (xslt-error "duplicate key: ~a" name
)
382 (setf (gethash name
(stylesheet-keys stylesheet
))
383 (make-key match use
))))
385 (defvar *excluded-namespaces
* (list *xsl
*))
386 (defvar *empty-mode
*)
387 (defvar *default-mode
*)
389 (defvar *xsl-include-stack
* nil
)
391 (defun uri-to-pathname (uri)
392 (cxml::uri-to-pathname
(puri:parse-uri uri
)))
394 (defun unwrap-2.3
(document)
395 (let ((literal-result-element (stp:document-element document
))
396 (new-template (stp:make-element
"template" *xsl
*))
397 (new-document-element (stp:make-element
"stylesheet" *xsl
*)))
398 (setf (stp:attribute-value new-document-element
"version")
399 (or (stp:attribute-value literal-result-element
"version" *xsl
*)
400 (xslt-error "not a stylesheet: root element lacks xsl:version")))
401 (setf (stp:attribute-value new-template
"match") "/")
402 (setf (stp:document-element document
) new-document-element
)
403 (stp:append-child new-document-element new-template
)
404 (stp:append-child new-template literal-result-element
)
405 new-document-element
))
407 (defun parse-stylesheet-to-stp (input uri-resolver
)
408 (let* ((d (cxml:parse input
(make-text-normalizer (cxml-stp:make-builder
))))
409 (<transform
> (stp:document-element d
)))
410 (unless (equal (stp:namespace-uri
<transform
>) *xsl
*)
411 (setf <transform
> (unwrap-2.3 d
)))
412 (strip-stylesheet <transform
>)
413 (unless (and (equal (stp:namespace-uri
<transform
>) *xsl
*)
414 (or (equal (stp:local-name
<transform
>) "transform")
415 (equal (stp:local-name
<transform
>) "stylesheet")))
416 (xslt-error "not a stylesheet"))
417 (dolist (include (stp:filter-children
(of-name "include") <transform
>))
418 (let* ((uri (puri:merge-uris
(stp:attribute-value include
"href")
419 (stp:base-uri include
)))
420 (uri (if uri-resolver
421 (funcall uri-resolver
(puri:render-uri uri nil
))
423 (str (puri:render-uri uri nil
))
426 (uri-to-pathname uri
)
427 (cxml:xml-parse-error
(c)
428 (xslt-error "cannot find included stylesheet ~A: ~A"
432 :element-type
'(unsigned-byte 8)
433 :if-does-not-exist nil
)
435 (xslt-error "cannot find included stylesheet ~A at ~A"
437 (when (find str
*xsl-include-stack
* :test
#'equal
)
438 (xslt-error "recursive inclusion of ~A" uri
))
439 (let* ((*xsl-include-stack
* (cons str
*xsl-include-stack
*))
440 (<transform
>2 (parse-stylesheet-to-stp stream uri-resolver
)))
441 (stp:insert-child-after
<transform
>
442 (stp:copy
<transform
>2)
444 (stp:detach include
)))))
447 (defvar *instruction-base-uri
*) ;misnamed, is also used in other attributes
448 (defvar *apply-imports-limit
*)
449 (defvar *import-priority
*)
450 (defvar *extension-namespaces
*)
451 (defvar *forwards-compatible-p
*)
453 (defmacro do-toplevel
((var xpath
<transform
>) &body body
)
454 `(map-toplevel (lambda (,var
) ,@body
) ,xpath
,<transform
>))
456 (defun map-toplevel (fn xpath
<transform
>)
457 (dolist (node (list-toplevel xpath
<transform
>))
458 (let ((*namespaces
* *namespaces
*))
459 (xpath:do-node-set
(ancestor (xpath:evaluate
"ancestor::node()" node
))
460 (when (xpath-protocol:node-type-p ancestor
:element
)
461 (setf *namespaces
* (acons-namespaces ancestor
))))
464 (defun list-toplevel (xpath <transform
>)
465 (labels ((recurse (sub)
468 (xpath:evaluate
"transform|stylesheet" sub
))))
470 (xpath-sys:pipe-of
(xpath:evaluate xpath sub
))
471 (xpath::mappend-pipe
#'recurse subsubs
)))))
472 (xpath::sort-nodes
(recurse <transform
>))))
474 (defmacro with-import-magic
((node env
) &body body
)
475 `(invoke-with-import-magic (lambda () ,@body
) ,node
,env
))
477 (defun invoke-with-import-magic (fn node env
)
478 (unless (or (namep node
"stylesheet") (namep node
"transform"))
479 (setf node
(stp:parent node
)))
480 (let ((*excluded-namespaces
* (list *xsl
*))
481 (*extension-namespaces
* '())
482 (*forwards-compatible-p
*
483 (not (equal (stp:attribute-value node
"version") "1.0"))))
484 (parse-exclude-result-prefixes! node env
)
485 (parse-extension-element-prefixes! node env
)
488 (defun parse-1-stylesheet (env stylesheet designator uri-resolver
)
489 (let* ((<transform
> (parse-stylesheet-to-stp designator uri-resolver
))
490 (instruction-base-uri (stp:base-uri
<transform
>))
491 (namespaces (acons-namespaces <transform
>))
492 (apply-imports-limit (1+ *import-priority
*))
494 (let ((*namespaces
* namespaces
))
495 (invoke-with-import-magic (constantly t
) <transform
> env
))
496 (macrolet ((with-specials ((&optional
) &body body
)
497 `(let ((*instruction-base-uri
* instruction-base-uri
)
498 (*namespaces
* namespaces
)
499 (*apply-imports-limit
* apply-imports-limit
))
502 (do-toplevel (import "import" <transform
>)
503 (let ((uri (puri:merge-uris
(stp:attribute-value import
"href")
504 (stp:base-uri import
))))
505 (push (parse-imported-stylesheet env stylesheet uri uri-resolver
)
507 (let ((import-priority
508 (incf *import-priority
*))
509 (var-cont (prepare-global-variables stylesheet
<transform
>)))
510 ;; delay the rest of compilation until we've seen all global
513 (mapc #'funcall
(nreverse continuations
))
515 (let ((*import-priority
* import-priority
))
517 (parse-keys! stylesheet
<transform
> env
)
518 (parse-templates! stylesheet
<transform
> env
)
519 (parse-output! stylesheet
<transform
>)
520 (parse-strip/preserve-space
! stylesheet
<transform
> env
)
521 (parse-attribute-sets! stylesheet
<transform
> env
)
522 (parse-namespace-aliases! stylesheet
<transform
> env
)
523 (parse-decimal-formats! stylesheet
<transform
> env
))))))))
525 (defvar *xsl-import-stack
* nil
)
527 (defun parse-imported-stylesheet (env stylesheet uri uri-resolver
)
528 (let* ((uri (if uri-resolver
529 (funcall uri-resolver
(puri:render-uri uri nil
))
531 (str (puri:render-uri uri nil
))
534 (uri-to-pathname uri
)
535 (cxml:xml-parse-error
(c)
536 (xslt-error "cannot find imported stylesheet ~A: ~A"
540 :element-type
'(unsigned-byte 8)
541 :if-does-not-exist nil
)
543 (xslt-error "cannot find imported stylesheet ~A at ~A"
545 (when (find str
*xsl-import-stack
* :test
#'equal
)
546 (xslt-error "recursive inclusion of ~A" uri
))
547 (let ((*xsl-import-stack
* (cons str
*xsl-import-stack
*)))
548 (parse-1-stylesheet env stylesheet stream uri-resolver
)))))
550 (defun parse-stylesheet (designator &key uri-resolver
)
551 (xpath:with-namespaces
((nil #.
*xsl
*))
552 (let* ((*import-priority
* 0)
553 (puri:*strict-parse
* nil
)
554 (stylesheet (make-stylesheet))
555 (env (make-instance 'lexical-xslt-environment
))
556 (*excluded-namespaces
* *excluded-namespaces
*)
557 (*global-variable-declarations
* (make-empty-declaration-array)))
558 (ensure-mode stylesheet nil
)
559 (funcall (parse-1-stylesheet env stylesheet designator uri-resolver
))
560 ;; reverse attribute sets:
561 (let ((table (stylesheet-attribute-sets stylesheet
)))
562 (maphash (lambda (k v
)
563 (setf (gethash k table
) (nreverse v
)))
566 (unless (find-decimal-format "" "" stylesheet nil
)
567 (setf (find-decimal-format "" "" stylesheet
)
568 (make-decimal-format)))
571 (defun parse-attribute-sets! (stylesheet <transform
> env
)
572 (do-toplevel (elt "attribute-set" <transform
>)
573 (with-import-magic (elt env
)
575 (mapcar (lambda (qname)
576 (multiple-value-list (decode-qname qname env nil
)))
578 (stp:attribute-value elt
"use-attribute-sets"))))
583 (unless (or (not (typep child
'stp
:element
))
584 (and (equal (stp:namespace-uri child
) *xsl
*)
585 (equal (stp:local-name child
)
587 (find (stp:namespace-uri child
)
588 *extension-namespaces
*
590 (xslt-error "non-attribute found in attribute set"))
591 (parse-instruction child
))
593 (*lexical-variable-declarations
*
594 (make-empty-declaration-array))
596 (compile-instruction `(progn ,@instructions
) env
))
597 (n-variables (length *lexical-variable-declarations
*)))
600 (loop for
(local-name uri nil
) in sets do
601 (dolist (thunk (find-attribute-set local-name uri
))
602 (funcall thunk ctx
)))
603 (let ((*lexical-variable-values
*
604 (make-variable-value-array n-variables
)))
605 (funcall thunk ctx
)))))
606 (gethash (multiple-value-bind (local-name uri
)
607 (decode-qname (stp:attribute-value elt
"name") env nil
)
608 (cons local-name uri
))
609 (stylesheet-attribute-sets stylesheet
))))))
611 (defun parse-namespace-aliases! (stylesheet <transform
> env
)
612 (do-toplevel (elt "namespace-alias" <transform
>)
613 (stp:with-attributes
(stylesheet-prefix result-prefix
) elt
615 (xpath-sys:environment-find-namespace env stylesheet-prefix
)
616 (stylesheet-namespace-aliases stylesheet
))
617 (xpath-sys:environment-find-namespace
619 (if (equal result-prefix
"#default")
623 (defun parse-decimal-formats! (stylesheet <transform
> env
)
624 (do-toplevel (elt "decimal-format" <transform
>)
625 (stp:with-attributes
(name
639 (multiple-value-bind (local-name uri
)
641 (decode-qname name env nil
)
643 (unless (find-decimal-format local-name uri stylesheet nil
)
644 (setf (find-decimal-format local-name uri stylesheet
)
648 (unless (eql (length x
) 1)
649 (xslt-error "not a single character: ~A" x
))
650 (let ((chr (elt x
0)))
651 (when (find chr seen
)
653 "conflicting decimal format characters: ~A"
660 (apply #'make-decimal-format
661 (append (str :infinity infinity
)
663 (chr :decimal-separator decimal-separator
)
664 (chr :grouping-separator grouping-separator
)
665 (chr :zero-digit zero-digit
)
666 (chr :percent percent
)
667 (chr :per-mille per-mille
)
669 (chr :pattern-separator pattern-separator
)
670 (chr :minus-sign minus-sign
)))))))))))
672 (defun parse-exclude-result-prefixes! (node env
)
673 (stp:with-attributes
(exclude-result-prefixes)
675 (dolist (prefix (words (or exclude-result-prefixes
"")))
676 (if (equal prefix
"#default")
678 (unless (cxml-stp-impl::nc-name-p prefix
)
679 (xslt-error "invalid prefix: ~A" prefix
)))
680 (push (or (xpath-sys:environment-find-namespace env prefix
)
681 (xslt-error "namespace not found: ~A" prefix
))
682 *excluded-namespaces
*))))
684 (defun parse-extension-element-prefixes! (node env
)
685 (stp:with-attributes
(extension-element-prefixes)
687 (dolist (prefix (words (or extension-element-prefixes
"")))
688 (if (equal prefix
"#default")
690 (unless (cxml-stp-impl::nc-name-p prefix
)
691 (xslt-error "invalid prefix: ~A" prefix
)))
693 (or (xpath-sys:environment-find-namespace env prefix
)
694 (xslt-error "namespace not found: ~A" prefix
))))
695 (unless (equal uri
*xsl
*)
696 (push uri
*extension-namespaces
*)
697 (push uri
*excluded-namespaces
*))))))
699 (defun parse-strip/preserve-space
! (stylesheet <transform
> env
)
700 (xpath:with-namespaces
((nil #.
*xsl
*))
701 (do-toplevel (elt "strip-space|preserve-space" <transform
>)
702 (let ((*namespaces
* (acons-namespaces elt
))
704 (if (equal (stp:local-name elt
) "strip-space")
707 (dolist (name-test (words (stp:attribute-value elt
"elements")))
708 (let* ((pos (search ":*" name-test
))
711 ((eql pos
(- (length name-test
) 2))
712 (let* ((prefix (subseq name-test
0 pos
))
714 (xpath-sys:environment-find-namespace env prefix
)))
715 (unless (xpath::nc-name-p prefix
)
716 (xslt-error "not an NCName: ~A" prefix
))
717 (lambda (local-name uri
)
718 (declare (ignore local-name
))
719 (if (equal uri name-test-uri
)
722 ((equal name-test
"*")
723 (lambda (local-name uri
)
724 (declare (ignore local-name uri
))
727 (multiple-value-bind (name-test-local-name name-test-uri
)
728 (decode-qname name-test env nil
)
729 (lambda (local-name uri
)
730 (if (and (equal local-name name-test-local-name
)
731 (equal uri name-test-uri
))
734 (push test-function
(stylesheet-strip-tests stylesheet
))))))))
736 (defstruct (output-specification
737 (:conc-name
"OUTPUT-"))
745 (defun parse-output! (stylesheet <transform
>)
746 (dolist (<output
> (list-toplevel "output" <transform
>))
747 (let ((spec (stylesheet-output-specification stylesheet
)))
748 (stp:with-attributes
( ;; version
757 ;;; cdata-section-elements
761 (setf (output-method spec
) method
))
763 (setf (output-indent spec
) indent
))
765 (setf (output-encoding spec
) encoding
))
767 (setf (output-doctype-system spec
) doctype-system
))
769 (setf (output-doctype-public spec
) doctype-public
))
770 (when omit-xml-declaration
771 (setf (output-omit-xml-declaration spec
) omit-xml-declaration
))
772 ;;; (when cdata-section-elements
773 ;;; (setf (output-cdata-section-elements spec)
774 ;;; (concatenate 'string
775 ;;; (output-cdata-section-elements spec)
777 ;;; cdata-section-elements)))
780 (defun make-empty-declaration-array ()
781 (make-array 1 :fill-pointer
0 :adjustable t
))
783 (defun make-variable-value-array (n-lexical-variables)
784 (make-array n-lexical-variables
:initial-element
'unbound
))
786 (defun compile-global-variable (<variable
> env
) ;; also for <param>
787 (stp:with-attributes
(name select
) <variable
>
788 (when (and select
(stp:list-children
<variable
>))
789 (xslt-error "variable with select and body"))
790 (let* ((*lexical-variable-declarations
* (make-empty-declaration-array))
793 (compile-xpath select env
))
794 ((stp:list-children
<variable
>)
795 (let* ((inner-sexpr `(progn ,@(parse-body <variable
>)))
796 (inner-thunk (compile-instruction inner-sexpr env
)))
798 (apply-to-result-tree-fragment ctx inner-thunk
))))
801 (declare (ignore ctx
))
803 (n-lexical-variables (length *lexical-variable-declarations
*)))
806 (let* ((*lexical-variable-values
*
807 (make-variable-value-array n-lexical-variables
)))
808 (funcall inner ctx
)))
809 "global ~s (~s) = ~s" name select
:result
))))
811 (defstruct (variable-information
812 (:constructor make-variable
)
813 (:conc-name
"VARIABLE-"))
821 (defun parse-global-variable! (<variable
> global-env
) ;; also for <param>
822 (let* ((*namespaces
* (acons-namespaces <variable
>))
823 (instruction-base-uri (stp:base-uri
<variable
>))
824 (*instruction-base-uri
* instruction-base-uri
)
825 (*excluded-namespaces
* (list *xsl
*))
826 (*extension-namespaces
* '())
827 (qname (stp:attribute-value
<variable
> "name")))
828 (with-import-magic (<variable
> global-env
)
830 (xslt-error "name missing in ~A" (stp:local-name
<variable
>)))
831 (multiple-value-bind (local-name uri
)
832 (decode-qname qname global-env nil
)
833 ;; For the normal compilation environment of templates, install it
834 ;; into *GLOBAL-VARIABLE-DECLARATIONS*:
835 (let ((index (intern-global-variable local-name uri
)))
836 ;; For the evaluation of a global variable itself, build a thunk
837 ;; that lazily resolves other variables, stored into
838 ;; INITIAL-GLOBAL-VARIABLE-THUNKS:
839 (let* ((value-thunk :unknown
)
840 (global-variable-thunk
842 (let ((v (global-variable-value index nil
)))
844 (xslt-error "recursive variable definition"))
847 (setf (global-variable-value index
) 'seen
)
848 (setf (global-variable-value index
)
849 (funcall value-thunk ctx
)))
852 (excluded-namespaces *excluded-namespaces
*)
853 (extension-namespaces *extension-namespaces
*)
856 (let ((*instruction-base-uri
* instruction-base-uri
)
857 (*excluded-namespaces
* excluded-namespaces
)
858 (*extension-namespaces
* extension-namespaces
))
860 (compile-global-variable <variable
> global-env
))))))
861 (setf (gethash (cons local-name uri
)
862 (initial-global-variable-thunks global-env
))
863 global-variable-thunk
)
864 (make-variable :index index
865 :local-name local-name
867 :thunk global-variable-thunk
868 :param-p
(namep <variable
> "param")
869 :thunk-setter thunk-setter
)))))))
871 (defun parse-keys! (stylesheet <transform
> env
)
872 (xpath:with-namespaces
((nil #.
*xsl
*))
873 (do-toplevel (<key
> "key" <transform
>)
874 (let ((*instruction-base-uri
* (stp:base-uri
<key
>)))
875 (stp:with-attributes
(name match use
) <key
>
876 (unless name
(xslt-error "key name attribute not specified"))
877 (unless match
(xslt-error "key match attribute not specified"))
878 (unless use
(xslt-error "key use attribute not specified"))
879 (multiple-value-bind (local-name uri
)
880 (decode-qname name env nil
)
882 (cons local-name uri
)
883 (compile-xpath `(xpath:xpath
,(parse-key-pattern match
)) env
)
884 (compile-xpath use env
))))))))
886 (defun prepare-global-variables (stylesheet <transform
>)
887 (xpath:with-namespaces
((nil #.
*xsl
*))
888 (let* ((table (make-hash-table :test
'equal
))
889 (global-env (make-instance 'global-variable-environment
890 :initial-global-variable-thunks table
))
892 (do-toplevel (<variable
> "variable|param" <transform
>)
893 (let ((var (parse-global-variable! <variable
> global-env
)))
894 (xslt-trace "parsing global variable ~s (uri ~s)"
895 (variable-local-name var
)
900 (and (equal (variable-local-name a
)
901 (variable-local-name b
))
902 (equal (variable-uri a
)
904 (xslt-error "duplicate definition for global variable ~A"
905 (variable-local-name var
)))
907 (setf specs
(nreverse specs
))
909 ;; now that the global environment knows about all variables, run the
910 ;; thunk setters to perform their compilation
911 (mapc (lambda (spec) (funcall (variable-thunk-setter spec
))) specs
)
912 (let ((table (stylesheet-global-variables stylesheet
))
913 (newlen (length *global-variable-declarations
*)))
914 (adjust-array table newlen
:fill-pointer newlen
)
916 (setf (elt table
(variable-index spec
)) spec
)))))))
918 (defun parse-templates! (stylesheet <transform
> env
)
920 (do-toplevel (<template
> "template" <transform
>)
921 (let ((*namespaces
* (acons-namespaces <template
>))
922 (*instruction-base-uri
* (stp:base-uri
<template
>)))
923 (with-import-magic (<template
> env
)
924 (dolist (template (compile-template <template
> env i
))
925 (let ((name (template-name template
)))
927 (let* ((table (stylesheet-named-templates stylesheet
))
928 (head (car (gethash name table
))))
929 (when (and head
(eql (template-import-priority head
)
930 (template-import-priority template
)))
931 ;; fixme: is this supposed to be a run-time error?
932 (xslt-error "conflicting templates for ~A" name
))
933 (push template
(gethash name table
)))
934 (let ((mode (ensure-mode/qname stylesheet
935 (template-mode-qname template
)
937 (setf (template-mode template
) mode
)
938 (push template
(mode-templates mode
))))))))
942 ;;;; APPLY-STYLESHEET
944 (defvar *stylesheet
*)
946 (deftype xml-designator
() '(or runes
:xstream runes
:rod array stream pathname
))
948 (defun unalias-uri (uri)
950 (gethash uri
(stylesheet-namespace-aliases *stylesheet
*)
952 (check-type result string
)
955 (defstruct (parameter
956 (:constructor make-parameter
(value local-name
&optional uri
)))
961 (defun find-parameter-value (local-name uri parameters
)
962 (dolist (p parameters
)
963 (when (and (equal (parameter-local-name p
) local-name
)
964 (equal (parameter-uri p
) uri
))
965 (return (parameter-value p
)))))
967 (defvar *uri-resolver
*)
969 (defun parse-allowing-microsoft-bom (pathname handler
)
970 (with-open-file (s pathname
:element-type
'(unsigned-byte 8))
971 (unless (and (eql (read-byte s nil
) #xef
)
972 (eql (read-byte s nil
) #xbb
)
973 (eql (read-byte s nil
) #xbf
))
975 (cxml:parse s handler
)))
979 (defun %document
(uri-string base-uri
)
981 (puri:merge-uris uri-string
(or base-uri
"")))
984 (funcall *uri-resolver
* (puri:render-uri absolute-uri nil
))
988 (uri-to-pathname resolved-uri
)
989 (cxml:xml-parse-error
(c)
990 (xslt-error "cannot find referenced document ~A: ~A"
993 (or (gethash pathname
*documents
*)
994 (setf (gethash pathname
*documents
*)
995 (make-whitespace-stripper
997 (parse-allowing-microsoft-bom pathname
999 ((or file-error cxml
:xml-parse-error
) (c)
1000 (xslt-error "cannot parse referenced document ~A: ~A"
1002 (stylesheet-strip-tests *stylesheet
*))))))
1003 (when (puri:uri-fragment absolute-uri
)
1004 (xslt-error "use of fragment identifiers in document() not supported"))
1007 (xpath-sys:define-extension xslt
*xsl
*)
1009 (defun document-base-uri (node)
1010 (xpath-protocol:base-uri
1012 ((xpath-protocol:node-type-p node
:document
)
1013 (xpath::find-in-pipe-if
1015 (xpath-protocol:node-type-p x
:element
))
1016 (xpath-protocol:child-pipe node
)))
1017 ((xpath-protocol:node-type-p node
:element
)
1020 (xpath-protocol:parent-node node
)))))
1022 (xpath-sys:define-xpath-function
/lazy
1024 (object &optional node-set
)
1025 (let ((instruction-base-uri *instruction-base-uri
*))
1027 (let* ((object (funcall object ctx
))
1028 (node-set (and node-set
(funcall node-set ctx
)))
1031 (document-base-uri (xpath::textually-first-node node-set
))
1032 instruction-base-uri
)))
1033 (xpath-sys:make-node-set
1034 (if (xpath:node-set-p object
)
1035 (xpath:map-node-set-
>list
1037 (%document
(xpath:string-value node
)
1040 (document-base-uri node
))))
1042 (list (%document
(xpath:string-value object
) base-uri
))))))))
1044 (xpath-sys:define-xpath-function
/lazy xslt
:key
(name object
)
1045 (let ((namespaces *namespaces
*))
1047 (let* ((qname (xpath:string-value
(funcall name ctx
)))
1048 (object (funcall object ctx
))
1050 (multiple-value-bind (local-name uri
)
1051 (decode-qname/runtime qname namespaces nil
)
1052 (cons local-name uri
)))
1053 (key (find-key expanded-name
*stylesheet
*)))
1054 (labels ((get-by-key (value)
1055 (let ((value (xpath:string-value value
)))
1059 (xpath:evaluate-compiled
(key-use key
) node
)))
1060 (if (xpath:node-set-p uses
)
1061 (xpath::find-in-pipe
1063 (xpath-sys:pipe-of uses
)
1064 :key
#'xpath
:string-value
1066 (equal value
(xpath:string-value uses
)))))
1068 (xpath:node-set-value
1069 (xpath:evaluate-compiled
(key-match key
) ctx
)))))))
1070 (xpath-sys:make-node-set
1072 (if (xpath:node-set-p object
)
1073 (xpath::mappend-pipe
#'get-by-key
(xpath-sys:pipe-of object
))
1074 (get-by-key object
)))))))))
1076 ;; FIXME: add alias mechanism for XPath extensions in order to avoid duplication
1078 (xpath-sys:define-xpath-function
/lazy xslt
:current
()
1080 (xpath-sys:make-node-set
1081 (xpath-sys:make-pipe
1082 (xpath:context-starting-node ctx
)
1085 (xpath-sys:define-xpath-function
/lazy xslt
:unparsed-entity-uri
(name)
1087 (or (xpath-protocol:unparsed-entity-uri
(xpath:context-node ctx
)
1091 (defun %get-node-id
(node)
1092 (when (xpath:node-set-p node
)
1093 (setf node
(xpath::textually-first-node node
)))
1095 (let ((id (xpath-sys:get-node-id node
))
1098 for parent
= node then next
1099 for next
= (xpath-protocol:parent-node parent
)
1100 for this-base-uri
= (xpath-protocol:base-uri parent
)
1101 for highest-base-uri
= (if (plusp (length this-base-uri
))
1105 finally
(return highest-base-uri
))))
1106 ;; Heuristic: Reverse it so that the /home/david/alwaysthesame prefix is
1107 ;; checked only if everything else matches.
1109 ;; This might be pointless premature optimization, but I like the idea :-)
1110 (nreverse (concatenate 'string highest-base-uri
"//" id
)))))
1112 (xpath-sys:define-xpath-function
/lazy xslt
:generate-id
(&optional node-set-thunk
)
1115 (%get-node-id
(xpath:node-set-value
(funcall node-set-thunk ctx
))))
1117 (%get-node-id
(xpath:context-node ctx
)))))
1119 (declaim (special *available-instructions
*))
1121 (xpath-sys:define-xpath-function
/lazy xslt
:element-available
(qname)
1122 (let ((namespaces *namespaces
*))
1124 (let ((qname (funcall qname ctx
)))
1125 (multiple-value-bind (local-name uri
)
1126 (decode-qname/runtime qname namespaces nil
)
1127 (and (equal uri
*xsl
*)
1128 (gethash local-name
*available-instructions
*)
1131 (xpath-sys:define-xpath-function
/lazy xslt
:function-available
(qname)
1132 (let ((namespaces *namespaces
*))
1134 (let ((qname (funcall qname ctx
)))
1135 (multiple-value-bind (local-name uri
)
1136 (decode-qname/runtime qname namespaces nil
)
1137 (and (zerop (length uri
))
1138 (or (xpath-sys:find-xpath-function local-name
*xsl
*)
1139 (xpath-sys:find-xpath-function local-name uri
))
1142 (xpath-sys:define-xpath-function
/lazy xslt
:system-property
(qname)
1143 (let ((namespaces *namespaces
*))
1145 (let ((qname (funcall qname ctx
)))
1146 (multiple-value-bind (local-name uri
)
1147 (decode-qname/runtime qname namespaces nil
)
1148 (if (equal uri
*xsl
*)
1150 ((equal local-name
"version")
1152 ((equal local-name
"vendor")
1154 ((equal local-name
"vendor-uri")
1155 "http://repo.or.cz/w/xuriella.git")
1160 (defun apply-stylesheet
1161 (stylesheet source-designator
1162 &key output parameters uri-resolver navigator
)
1163 (when (typep stylesheet
'xml-designator
)
1164 (setf stylesheet
(parse-stylesheet stylesheet
)))
1165 (invoke-with-output-sink
1168 (let* ((*documents
* (make-hash-table :test
'equal
))
1169 (xpath:*navigator
* (or navigator
:default-navigator
))
1170 (puri:*strict-parse
* nil
)
1171 (*stylesheet
* stylesheet
)
1172 (*empty-mode
* (make-mode))
1173 (*default-mode
* (find-mode stylesheet nil
))
1174 (global-variable-specs
1175 (stylesheet-global-variables stylesheet
))
1176 (*global-variable-values
*
1177 (make-variable-value-array (length global-variable-specs
)))
1178 (*uri-resolver
* uri-resolver
)
1180 (if (typep source-designator
'xml-designator
)
1181 (cxml:parse source-designator
(stp:make-builder
))
1184 (make-whitespace-stripper
1186 (stylesheet-strip-tests stylesheet
)))
1187 (ctx (xpath:make-context xpath-root-node
)))
1188 (when (pathnamep source-designator
)
1189 (setf (gethash source-designator
*documents
*) xpath-root-node
))
1192 (when (variable-param-p spec
)
1194 (find-parameter-value (variable-local-name spec
)
1198 (setf (global-variable-value (variable-index spec
))
1200 global-variable-specs
)
1203 (funcall (variable-thunk spec
) ctx
))
1204 global-variable-specs
)
1205 ;; zzz we wouldn't have to mask float traps here if we used the
1206 ;; XPath API properly. Unfortunately I've been using FUNCALL
1207 ;; everywhere instead of EVALUATE, so let's paper over that
1208 ;; at a central place to be sure:
1209 (xpath::with-float-traps-masked
()
1210 (apply-templates ctx
:mode
*default-mode
*)))
1211 (xpath:xpath-error
(c)
1212 (xslt-error "~A" c
))))
1213 (stylesheet-output-specification stylesheet
)
1216 (defun find-attribute-set (local-name uri
)
1217 (or (gethash (cons local-name uri
) (stylesheet-attribute-sets *stylesheet
*))
1218 (xslt-error "no such attribute set: ~A/~A" local-name uri
)))
1220 (defun apply-templates/list
(list &key param-bindings sort-predicate mode
)
1221 (when sort-predicate
1223 (mapcar #'xpath
:context-node
1224 (stable-sort (contextify-node-list list
)
1226 (let* ((n (length list
))
1227 (s/d
(lambda () n
)))
1232 (apply-templates (xpath:make-context child s
/d i
)
1233 :param-bindings param-bindings
1236 (defvar *stack-limit
* 200)
1238 (defun invoke-with-stack-limit (fn)
1239 (let ((*stack-limit
* (1- *stack-limit
*)))
1240 (unless (plusp *stack-limit
*)
1241 (xslt-error "*stack-limit* reached; stack overflow"))
1244 (defun invoke-template (ctx template param-bindings
)
1245 (let ((*lexical-variable-values
*
1246 (make-variable-value-array (template-n-variables template
))))
1247 (with-stack-limit ()
1249 for
(name-cons value
) in param-bindings
1250 for
(nil index nil
) = (find name-cons
1251 (template-params template
)
1256 (setf (lexical-variable-value index
) value
)))
1257 (funcall (template-body template
) ctx
))))
1259 (defun apply-default-templates (ctx mode
)
1260 (let ((node (xpath:context-node ctx
)))
1262 ((or (xpath-protocol:node-type-p node
:processing-instruction
)
1263 (xpath-protocol:node-type-p node
:comment
)))
1264 ((or (xpath-protocol:node-type-p node
:text
)
1265 (xpath-protocol:node-type-p node
:attribute
))
1266 (write-text (xpath-protocol:node-text node
)))
1268 (apply-templates/list
1270 (xpath-protocol:child-pipe node
))
1273 (defvar *apply-imports
*)
1275 (defun apply-applicable-templates (ctx templates param-bindings finally
)
1276 (labels ((apply-imports (&optional actual-param-bindings
)
1278 (let* ((this (pop templates
))
1279 (low (template-apply-imports-limit this
))
1280 (high (template-import-priority this
)))
1284 (<= low
(template-import-priority x
) high
))
1286 (invoke-template ctx this actual-param-bindings
))
1287 (funcall finally
))))
1288 (let ((*apply-imports
* #'apply-imports
))
1289 (apply-imports param-bindings
))))
1291 (defun apply-templates (ctx &key param-bindings mode
)
1292 (apply-applicable-templates ctx
1293 (find-templates ctx
(or mode
*default-mode
*))
1296 (apply-default-templates ctx mode
))))
1298 (defun call-template (ctx name
&optional param-bindings
)
1299 (apply-applicable-templates ctx
1300 (find-named-templates name
)
1303 (error "cannot find named template: ~s"
1306 (defun find-templates (ctx mode
)
1307 (let* ((matching-candidates
1308 (remove-if-not (lambda (template)
1309 (template-matches-p template ctx
))
1310 (mode-templates mode
)))
1312 (if matching-candidates
1315 :key
#'template-import-priority
))
1317 (priority-groups (make-array npriorities
:initial-element nil
)))
1318 (dolist (template matching-candidates
)
1320 (elt priority-groups
(template-import-priority template
))))
1322 for i from
(1- npriorities
) downto
0
1323 for group
= (elt priority-groups i
)
1324 for template
= (maximize #'template
< group
)
1328 (defun find-named-templates (name)
1329 (gethash name
(stylesheet-named-templates *stylesheet
*)))
1331 (defun template< (a b
) ;assuming same import priority
1332 (let ((p (template-priority a
))
1333 (q (template-priority b
)))
1338 (xslt-cerror "conflicting templates:~_~A,~_~A"
1339 (template-match-expression a
)
1340 (template-match-expression b
))
1341 (< (template-position a
) (template-position b
))))))
1343 (defun maximize (< things
)
1345 (let ((max (car things
)))
1346 (dolist (other (cdr things
))
1347 (when (funcall < max other
)
1351 (defun template-matches-p (template ctx
)
1352 (find (xpath:context-node ctx
)
1353 (xpath:all-nodes
(funcall (template-match-thunk template
) ctx
))
1354 :test
#'xpath-protocol
:node-equal
))
1356 (defun invoke-with-output-sink (fn output-spec output
)
1359 (with-open-file (s output
1361 :element-type
'(unsigned-byte 8)
1362 :if-exists
:rename-and-delete
)
1363 (invoke-with-output-sink fn output-spec s
)))
1365 (invoke-with-output-sink fn
1367 (make-output-sink output-spec output
)))
1368 ((or hax
:abstract-handler sax
:abstract-handler
)
1369 (with-xml-output output
1370 (when (typep output
'(or combi-sink auto-detect-sink
))
1371 (sax:start-dtd output
1372 :autodetect-me-please
1373 (output-doctype-public output-spec
)
1374 (output-doctype-system output-spec
)))
1377 (defun make-output-sink (output-spec stream
)
1380 (let ((et (stream-element-type stream
)))
1382 ((or (null et
) (subtypep et
'(unsigned-byte 8)))
1383 (runes:make-octet-stream-ystream stream
))
1384 ((subtypep et
'character
)
1385 (runes:make-character-stream-ystream stream
))))
1386 (runes:make-rod-ystream
)))
1387 (omit-xml-declaration-p
1388 (equal (output-omit-xml-declaration output-spec
) "yes"))
1390 (make-instance 'cxml
::sink
1392 :omit-xml-declaration-p omit-xml-declaration-p
)))
1393 (flet ((make-combi-sink ()
1394 (make-instance 'combi-sink
1395 :hax-target
(make-instance 'chtml
::sink
1397 :sax-target sax-target
1398 :encoding
(output-encoding output-spec
))))
1401 ((equalp (output-method output-spec
) "HTML") :html
)
1402 ((equalp (output-method output-spec
) "TEXT") :text
)
1403 ((equalp (output-method output-spec
) "XML") :xml
)
1406 ((and (eq method-key
:html
)
1407 (null (output-doctype-system output-spec
))
1408 (null (output-doctype-public output-spec
)))
1410 ((eq method-key
:text
)
1411 (make-text-filter sax-target
))
1412 ((and (eq method-key
:xml
)
1413 (null (output-doctype-system output-spec
)))
1416 (make-auto-detect-sink (make-combi-sink) method-key
)))))))
1432 (defun expression-priority (form)
1433 (let ((step (second form
)))
1434 (if (and (null (cddr form
))
1436 (member (car step
) '(:child
:attribute
))
1438 (let ((name (second step
)))
1442 (or (eq (car name
) :qname
)
1443 (eq (car name
) :processing-instruction
))))
1446 (or (eq (car name
) :namespace
)
1447 (eq (car name
) '*)))
1453 (defun valid-expression-p (expr)
1456 ((eq (first expr
) :path
)
1458 (let ((filter (third x
)))
1459 (or (null filter
) (valid-expression-p filter
))))
1461 ((eq (first expr
) :variable
) ;(!)
1464 (every #'valid-expression-p
(cdr expr
)))))
1466 (defun parse-xpath (str)
1468 (xpath:parse-xpath str
)
1469 (xpath:xpath-error
(c)
1470 (xslt-error "~A" c
))))
1472 ;; zzz also use naive-pattern-expression here?
1473 (defun parse-key-pattern (str)
1475 (mapcar #'(lambda (item)
1476 `(:path
(:root
:node
)
1477 (:descendant-or-self
*)
1479 (parse-pattern str
))))
1480 (if (null (rest parsed
))
1482 `(:union
,@parsed
))))
1484 (defun parse-pattern (str)
1485 ;; zzz check here for anything not allowed as an XSLT pattern
1486 ;; zzz can we hack id() and key() here?
1487 (let ((form (parse-xpath str
)))
1488 (unless (consp form
)
1489 (xslt-error "not a valid pattern: ~A" str
))
1490 (labels ((process-form (form)
1491 (cond ((eq (car form
) :union
)
1492 (alexandria:mappend
#'process-form
(rest form
)))
1493 ((not (or (eq (car form
) :path
)
1494 (and (eq (car form
) :filter
)
1495 (let ((filter (second form
)))
1497 (member (car filter
)
1499 (equal (third form
) '(:true
)))
1500 (member (car form
) '(:key
:id
))))
1501 (xslt-error "not a valid pattern: ~A ~A" str form
))
1502 ((not (valid-expression-p form
))
1503 (xslt-error "invalid filter"))
1505 (process-form form
))))
1507 (defun naive-pattern-expression (x)
1509 (:path
`(:path
(:ancestor-or-self
:node
) ,@(cdr x
)))
1510 ((:filter
:key
:id
) x
)))
1512 (defun compile-value-thunk (value env
)
1513 (if (and (listp value
) (eq (car value
) 'progn
))
1514 (let ((inner-thunk (compile-instruction value env
)))
1516 (apply-to-result-tree-fragment ctx inner-thunk
)))
1517 (compile-xpath value env
)))
1519 (defun compile-var-binding (name value env
)
1520 (multiple-value-bind (local-name uri
)
1521 (decode-qname name env nil
)
1522 (let ((thunk (xslt-trace-thunk
1523 (compile-value-thunk value env
)
1524 "local variable ~s = ~s" name
:result
)))
1525 (list (cons local-name uri
)
1526 (push-variable local-name
1528 *lexical-variable-declarations
*)
1531 (defun compile-var-bindings (forms env
)
1533 for
(name value
) in forms
1534 collect
(compile-var-binding name value env
)))
1536 (defun compile-template (<template
> env position
)
1537 (stp:with-attributes
(match name priority mode
) <template
>
1538 (unless (or name match
)
1539 (xslt-error "missing match in template"))
1540 (multiple-value-bind (params body-pos
)
1543 for child in
(stp:list-children
<template
>)
1544 while
(namep child
"param")
1545 collect
(parse-param child
) into params
1546 finally
(return (values params i
)))
1547 (let* ((*lexical-variable-declarations
* (make-empty-declaration-array))
1548 (param-bindings (compile-var-bindings params env
))
1549 (body (parse-body <template
> body-pos
(mapcar #'car params
)))
1550 (body-thunk (compile-instruction `(progn ,@body
) env
))
1556 ;; set params that weren't initialized by apply-templates
1557 (loop for
(name index param-thunk
) in param-bindings
1558 when
(eq (lexical-variable-value index nil
) 'unbound
)
1559 do
(setf (lexical-variable-value index
)
1560 (funcall param-thunk ctx
)))
1561 (funcall body-thunk ctx
))))
1562 "template: match = ~s name = ~s" match name
))
1563 (n-variables (length *lexical-variable-declarations
*)))
1566 (multiple-value-bind (local-name uri
)
1567 (decode-qname name env nil
)
1569 (make-template :name
(cons local-name uri
)
1570 :import-priority
*import-priority
*
1571 :apply-imports-limit
*apply-imports-limit
*
1572 :params param-bindings
1573 :body outer-body-thunk
1574 :n-variables n-variables
))))
1576 (mapcar (lambda (expression)
1581 ,(naive-pattern-expression expression
))
1583 "match-thunk for template (match ~s): ~s --> ~s"
1584 match expression
:result
))
1586 (parse-number:parse-number priority
)
1587 (expression-priority expression
))))
1588 (make-template :match-expression expression
1589 :match-thunk match-thunk
1590 :import-priority
*import-priority
*
1591 :apply-imports-limit
*apply-imports-limit
*
1595 :params param-bindings
1596 :body outer-body-thunk
1597 :n-variables n-variables
)))
1598 (parse-pattern match
))))))))
1600 (xuriella::parse-stylesheet
#p
"/home/david/src/lisp/xuriella/test.xsl")