Dubious namespace alias workarounds:
[xuriella.git] / xslt.lisp
blobf6f49e111d527c888253af332e1be391603090b0
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
8 ;;; are met:
9 ;;;
10 ;;; * Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
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.
17 ;;;
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)
32 #+sbcl
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"))
42 ;;;; XSLT-ERROR
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 ;; Many errors in XSLT are "recoverable", with a specified action that must
56 ;; be taken if the error isn't raised. My original plan was to implement
57 ;; such issues as continuable conditions, so that users are alerted about
58 ;; portability issues with their stylesheet, but can contiue anyway.
60 ;; However, our current test suite driver compares against Saxon results,
61 ;; and Saxon recovers (nearly) always. So our coverage of these errors
62 ;; is very incomplete.
64 ;; Re-enable this code once we can check that it's actually being used
65 ;; everywhere.
66 (defun xslt-cerror (fmt &rest args)
67 (declare (ignore fmt args))
68 #+(or)
69 (with-simple-restart (recover "recover")
70 (error 'recoverable-xslt-error
71 :format-control fmt
72 :format-arguments args)))
74 (defvar *debug* nil)
76 (defmacro handler-case* (form &rest clauses)
77 ;; like HANDLER-CASE if *DEBUG* is off. If it's on, don't establish
78 ;; a handler at all so that we see the real stack traces. (We could use
79 ;; HANDLER-BIND here and check at signalling time, but doesn't seem
80 ;; important.)
81 (let ((doit (gensym)))
82 `(flet ((,doit () ,form))
83 (if *debug*
84 (,doit)
85 (handler-case
86 (,doit)
87 ,@clauses)))))
89 (defmacro with-resignalled-errors ((&optional) &body body)
90 `(invoke-with-resignalled-errors (lambda () ,@body)))
92 (defun invoke-with-resignalled-errors (fn)
93 (handler-bind
94 ((xpath:xpath-error
95 (lambda (c)
96 (xslt-error "~A" c)))
97 (babel-encodings:character-encoding-error
98 (lambda (c)
99 (xslt-error "~A" c))))
100 (funcall fn)))
102 (defmacro with-forward-compatible-errors (error-form &body body)
103 `(invoke-with-forward-compatible-errors (lambda () ,@body)
104 (lambda () ,error-form)))
106 (defun invoke-with-forward-compatible-errors (fn error-fn)
107 (let ((result))
108 (tagbody
109 (handler-bind
110 ((xpath:xpath-error
111 (lambda (c)
112 (declare (ignore c))
113 (when *forwards-compatible-p*
114 (go error)))))
115 (setf result (funcall fn)))
116 (go done)
117 error
118 (setf result (funcall error-fn))
119 done)
120 result))
122 (defun compile-xpath (xpath &optional env)
123 (with-resignalled-errors ()
124 (with-forward-compatible-errors
125 (lambda (ctx)
126 (xslt-error "attempt to evaluate an XPath expression with compile-time errors, delayed due to forwards compatible processing: ~A"
127 xpath))
128 (xpath:compile-xpath xpath env))))
130 (defmacro with-stack-limit ((&optional) &body body)
131 `(invoke-with-stack-limit (lambda () ,@body)))
133 (defparameter *without-xslt-current-p* nil)
135 (defmacro without-xslt-current ((&optional) &body body)
136 `(invoke-without-xslt-current (lambda () ,@body)))
138 (defun invoke-without-xslt-current (fn)
139 (let ((*without-xslt-current-p* t))
140 (funcall fn)))
142 ;;; (defun invoke-without-xslt-current (fn)
143 ;;; (let ((non-extensions (gethash "" xpath::*extensions*))
144 ;;; (xpath::*extensions*
145 ;;; ;; hide XSLT extensions
146 ;;; (make-hash-table :test #'equal)))
147 ;;; (setf (gethash "" xpath::*extensions*) non-extensions)
148 ;;; (funcall fn)))
151 ;;;; Helper functions and macros
153 (defun check-for-invalid-attributes (valid-names node)
154 (labels ((check-attribute (a)
155 (unless
156 (let ((uri (stp:namespace-uri a)))
157 (or (and (plusp (length uri)) (not (equal uri *xsl*)))
158 (find (cons (stp:local-name a) uri)
159 valid-names
160 :test #'equal)))
161 (xslt-error "attribute ~A not allowed on ~A"
162 (stp:local-name a)
163 (stp:local-name node)))))
164 (stp:map-attributes nil #'check-attribute node)))
166 (defmacro only-with-attributes ((&rest specs) node &body body)
167 (let ((valid-names
168 (mapcar (lambda (entry)
169 (if (and (listp entry) (cdr entry))
170 (destructuring-bind (name &optional (uri ""))
171 (cdr entry)
172 (cons name uri))
173 (cons (string-downcase
174 (princ-to-string
175 (symbol-name entry)))
176 "")))
177 specs))
178 (%node (gensym)))
179 `(let ((,%NODE ,node))
180 (check-for-invalid-attributes ',valid-names ,%NODE)
181 (stp:with-attributes ,specs ,%NODE
182 ,@body))))
184 (defun map-pipe-eagerly (fn pipe)
185 (xpath::enumerate pipe :key fn :result nil))
187 (defmacro do-pipe ((var pipe &optional result) &body body)
188 `(block nil
189 (map-pipe-eagerly #'(lambda (,var) ,@body) ,pipe)
190 ,result))
193 ;;;; XSLT-ENVIRONMENT and XSLT-CONTEXT
195 (defparameter *initial-namespaces*
196 '((nil . "")
197 ("xmlns" . #"http://www.w3.org/2000/xmlns/")
198 ("xml" . #"http://www.w3.org/XML/1998/namespace")))
200 (defparameter *namespaces*
201 *initial-namespaces*)
203 (defvar *global-variable-declarations*)
204 (defvar *lexical-variable-declarations*)
206 (defvar *global-variable-values*)
207 (defvar *lexical-variable-values*)
209 (defclass xslt-environment () ())
211 (defun split-qname (str)
212 (handler-case
213 (multiple-value-bind (prefix local-name)
214 (cxml::split-qname str)
215 (unless
216 ;; FIXME: cxml should really offer a function that does
217 ;; checks for NCName and QName in a sensible way for user code.
218 ;; cxml::split-qname is tailored to the needs of the parser.
220 ;; For now, let's just check the syntax explicitly.
221 (and (or (null prefix) (xpath::nc-name-p prefix))
222 (xpath::nc-name-p local-name))
223 (xslt-error "not a qname: ~A" str))
224 (values prefix local-name))
225 (cxml:well-formedness-violation ()
226 (xslt-error "not a qname: ~A" str))))
228 (defun decode-qname (qname env attributep &key allow-unknown-namespace)
229 (unless qname
230 (xslt-error "missing name"))
231 (multiple-value-bind (prefix local-name)
232 (split-qname qname)
233 (values local-name
234 (if (or prefix (not attributep))
235 (or (xpath-sys:environment-find-namespace env (or prefix ""))
236 (if allow-unknown-namespace
238 (xslt-error "namespace not found: ~A" prefix)))
240 prefix)))
242 (defmethod xpath-sys:environment-find-namespace ((env xslt-environment) prefix)
243 (or (cdr (assoc prefix *namespaces* :test 'equal))
244 ;; zzz gross hack.
245 ;; Change the entire code base to represent "no prefix" as the
246 ;; empty string consistently. unparse.lisp has already been changed.
247 (and (equal prefix "")
248 (cdr (assoc nil *namespaces* :test 'equal)))
249 (and (eql prefix nil)
250 (cdr (assoc "" *namespaces* :test 'equal)))))
252 (defun find-variable-index (local-name uri table)
253 (position (cons local-name uri) table :test 'equal))
255 (defun intern-global-variable (local-name uri)
256 (or (find-variable-index local-name uri *global-variable-declarations*)
257 (push-variable local-name uri *global-variable-declarations*)))
259 (defun push-variable (local-name uri table)
260 (prog1
261 (length table)
262 (vector-push-extend (cons local-name uri) table)))
264 (defun lexical-variable-value (index &optional (errorp t))
265 (let ((result (svref *lexical-variable-values* index)))
266 (when errorp
267 (assert (not (eq result 'unbound))))
268 result))
270 (defun (setf lexical-variable-value) (newval index)
271 (assert (not (eq newval 'unbound)))
272 (setf (svref *lexical-variable-values* index) newval))
274 (defun global-variable-value (index &optional (errorp t))
275 (let ((result (svref *global-variable-values* index)))
276 (when errorp
277 (assert (not (eq result 'unbound))))
278 result))
280 (defun (setf global-variable-value) (newval index)
281 (assert (not (eq newval 'unbound)))
282 (setf (svref *global-variable-values* index) newval))
284 (defmethod xpath-sys:environment-find-function
285 ((env xslt-environment) lname uri)
286 (or (if (string= uri "")
287 (or (xpath-sys:find-xpath-function lname *xsl*)
288 (xpath-sys:find-xpath-function lname uri))
289 (xpath-sys:find-xpath-function lname uri))
290 (when *forwards-compatible-p*
291 (lambda (&rest ignore)
292 (declare (ignore ignore))
293 (xslt-error "attempt to call an unknown XPath function (~A); error delayed until run-time due to forwards compatible processing"
294 lname)))))
296 (defmethod xpath-sys:environment-find-variable
297 ((env xslt-environment) lname uri)
298 (let ((index
299 (find-variable-index lname uri *lexical-variable-declarations*)))
300 (when index
301 (lambda (ctx)
302 (declare (ignore ctx))
303 (svref *lexical-variable-values* index)))))
305 (defclass lexical-xslt-environment (xslt-environment) ())
307 (defmethod xpath-sys:environment-find-variable
308 ((env lexical-xslt-environment) lname uri)
309 (or (call-next-method)
310 (let ((index
311 (find-variable-index lname uri *global-variable-declarations*)))
312 (when index
313 (xslt-trace-thunk
314 (lambda (ctx)
315 (declare (ignore ctx))
316 (svref *global-variable-values* index))
317 "global ~s (uri ~s) = ~s" lname uri :result)))))
319 (defclass key-environment (xslt-environment) ())
321 (defmethod xpath-sys:environment-find-variable
322 ((env key-environment) lname uri)
323 (declare (ignore lname uri))
324 (xslt-error "disallowed variable reference"))
326 (defclass global-variable-environment (xslt-environment)
327 ((initial-global-variable-thunks
328 :initarg :initial-global-variable-thunks
329 :accessor initial-global-variable-thunks)))
331 (defmethod xpath-sys:environment-find-variable
332 ((env global-variable-environment) lname uri)
333 (or (call-next-method)
334 (gethash (cons lname uri) (initial-global-variable-thunks env))))
337 ;;;; TOPLEVEL-TEXT-OUTPUT-SINK
338 ;;;;
339 ;;;; A sink that serializes only text not contained in any element.
341 (defmacro with-toplevel-text-output-sink ((var) &body body)
342 `(invoke-with-toplevel-text-output-sink (lambda (,var) ,@body)))
344 (defclass toplevel-text-output-sink (sax:default-handler)
345 ((target :initarg :target :accessor text-output-sink-target)
346 (depth :initform 0 :accessor textoutput-sink-depth)))
348 (defmethod sax:start-element ((sink toplevel-text-output-sink)
349 namespace-uri local-name qname attributes)
350 (declare (ignore namespace-uri local-name qname attributes))
351 (incf (textoutput-sink-depth sink)))
353 (defmethod sax:characters ((sink toplevel-text-output-sink) data)
354 (when (zerop (textoutput-sink-depth sink))
355 (write-string data (text-output-sink-target sink))))
357 (defmethod sax:unescaped ((sink toplevel-text-output-sink) data)
358 (sax:characters sink data))
360 (defmethod sax:end-element ((sink toplevel-text-output-sink)
361 namespace-uri local-name qname)
362 (declare (ignore namespace-uri local-name qname))
363 (decf (textoutput-sink-depth sink)))
365 (defun invoke-with-toplevel-text-output-sink (fn)
366 (with-output-to-string (s)
367 (funcall fn (make-instance 'toplevel-text-output-sink :target s))))
370 ;;;; TEXT-FILTER
371 ;;;;
372 ;;;; A sink that passes through only text (at any level) and turns to
373 ;;;; into unescaped characters.
375 (defclass text-filter (sax:default-handler)
376 ((target :initarg :target :accessor text-filter-target)))
378 (defmethod sax:characters ((sink text-filter) data)
379 (sax:unescaped (text-filter-target sink) data))
381 (defmethod sax:unescaped ((sink text-filter) data)
382 (sax:unescaped (text-filter-target sink) data))
384 (defmethod sax:end-document ((sink text-filter))
385 (sax:end-document (text-filter-target sink)))
387 (defun make-text-filter (target)
388 (make-instance 'text-filter :target target))
391 ;;;; ESCAPER
392 ;;;;
393 ;;;; A sink that recovers from sax:unescaped using sax:characters, as per
394 ;;;; XSLT 16.4.
396 (defclass escaper (cxml:broadcast-handler)
399 (defmethod sax:unescaped ((sink escaper) data)
400 (sax:characters sink data))
402 (defun make-escaper (target)
403 (make-instance 'escaper :handlers (list target)))
406 ;;;; Names
408 (defun of-name (local-name)
409 (stp:of-name local-name *xsl*))
411 (defun namep (node local-name)
412 (and (typep node '(or stp:element stp:attribute))
413 (equal (stp:namespace-uri node) *xsl*)
414 (equal (stp:local-name node) local-name)))
417 ;;;; PARSE-STYLESHEET
419 (defstruct stylesheet
420 (modes (make-hash-table :test 'equal))
421 (global-variables (make-empty-declaration-array))
422 (output-specification (make-output-specification))
423 (strip-tests nil)
424 (strip-thunk nil)
425 (named-templates (make-hash-table :test 'equal))
426 (attribute-sets (make-hash-table :test 'equal))
427 (keys (make-hash-table :test 'equal))
428 (namespace-aliases (make-hash-table :test 'equal))
429 (decimal-formats (make-hash-table :test 'equal))
430 (initial-global-variable-thunks (make-hash-table :test 'equal)))
432 (defstruct mode
433 (templates nil)
434 (match-thunk (lambda (ignore) (declare (ignore ignore)) nil)))
436 (defun find-mode (stylesheet local-name &optional uri)
437 (gethash (cons local-name uri) (stylesheet-modes stylesheet)))
439 (defun ensure-mode (stylesheet &optional local-name uri)
440 (or (find-mode stylesheet local-name uri)
441 (setf (gethash (cons local-name uri) (stylesheet-modes stylesheet))
442 (make-mode))))
444 (defun ensure-mode/qname (stylesheet qname env)
445 (if qname
446 (multiple-value-bind (local-name uri)
447 (decode-qname qname env nil)
448 (ensure-mode stylesheet local-name uri))
449 (find-mode stylesheet nil)))
451 (defun acons-namespaces
452 (element &optional (bindings *namespaces*) include-redeclared)
453 (map-namespace-declarations (lambda (prefix uri)
454 (push (cons prefix uri) bindings))
455 element
456 include-redeclared)
457 bindings)
459 (defun find-key (name stylesheet)
460 (or (gethash name (stylesheet-keys stylesheet))
461 (xslt-error "unknown key: ~a" name)))
463 (defun make-key (match use) (cons match use))
465 (defun key-match (key) (car key))
467 (defun key-use (key) (cdr key))
469 (defun add-key (stylesheet name match use)
470 (push (make-key match use)
471 (gethash name (stylesheet-keys stylesheet))))
473 (defvar *excluded-namespaces* (list *xsl*))
474 (defvar *empty-mode*)
475 (defvar *default-mode*)
477 (defvar *xsl-include-stack* nil)
479 (defun uri-to-pathname (uri)
480 (cxml::uri-to-pathname (puri:parse-uri uri)))
482 ;; Why this extra check for literal result element used as stylesheets,
483 ;; instead of a general check for every literal result element? Because
484 ;; Stylesheet__91804 says so.
485 (defun check-Errors_err035 (literal-result-element)
486 (let ((*namespaces* (acons-namespaces literal-result-element))
487 (env (make-instance 'lexical-xslt-environment)))
488 (stp:with-attributes ((extension-element-prefixes
489 "extension-element-prefixes"
490 *xsl*))
491 literal-result-element
492 (dolist (prefix (words (or extension-element-prefixes "")))
493 (if (equal prefix "#default")
494 (setf prefix nil)
495 (unless (cxml-stp-impl::nc-name-p prefix)
496 (xslt-error "invalid prefix: ~A" prefix)))
497 (let ((uri
498 (or (xpath-sys:environment-find-namespace env prefix)
499 (xslt-error "namespace not found: ~A" prefix))))
500 (when (equal uri (stp:namespace-uri literal-result-element))
501 (xslt-error "literal result element used as stylesheet, but is ~
502 declared as an extension element")))))))
504 (defun unwrap-2.3 (document)
505 (let ((literal-result-element (stp:document-element document))
506 (new-template (stp:make-element "template" *xsl*))
507 (new-document-element (stp:make-element "stylesheet" *xsl*)))
508 (check-Errors_err035 literal-result-element)
509 (setf (stp:attribute-value new-document-element "version")
510 (or (stp:attribute-value literal-result-element "version" *xsl*)
511 (xslt-error "not a stylesheet: root element lacks xsl:version")))
512 (setf (stp:attribute-value new-template "match") "/")
513 (setf (stp:document-element document) new-document-element)
514 (stp:append-child new-document-element new-template)
515 (stp:append-child new-template literal-result-element)
516 new-document-element))
518 (defun parse-stylesheet-to-stp (input uri-resolver)
519 (let* ((d (cxml:parse input (make-text-normalizer (cxml-stp:make-builder))))
520 (<transform> (stp:document-element d)))
521 (unless (equal (stp:namespace-uri <transform>) *xsl*)
522 (setf <transform> (unwrap-2.3 d)))
523 (strip-stylesheet <transform>)
524 (unless (and (equal (stp:namespace-uri <transform>) *xsl*)
525 (or (equal (stp:local-name <transform>) "transform")
526 (equal (stp:local-name <transform>) "stylesheet")))
527 (xslt-error "not a stylesheet"))
528 (check-for-invalid-attributes
529 '(("version" . "")
530 ("exclude-result-prefixes" . "")
531 ("extension-element-prefixes" . "")
532 ("space" . "http://www.w3.org/XML/1998/namespace")
533 ("id" . ""))
534 <transform>)
535 (let ((invalid
536 (or (stp:find-child-if (of-name "stylesheet") <transform>)
537 (stp:find-child-if (of-name "transform") <transform>))))
538 (when invalid
539 (xslt-error "invalid top-level element ~A" (stp:local-name invalid))))
540 (dolist (include (stp:filter-children (of-name "include") <transform>))
541 (let* ((uri (puri:merge-uris (or (stp:attribute-value include "href")
542 (xslt-error "include without href"))
543 (stp:base-uri include)))
544 (uri (if uri-resolver
545 (funcall uri-resolver uri)
546 uri))
547 (str (puri:render-uri uri nil))
548 (pathname
549 (handler-case
550 (uri-to-pathname uri)
551 (cxml:xml-parse-error (c)
552 (xslt-error "cannot find included stylesheet ~A: ~A"
553 uri c)))))
554 (with-open-file
555 (stream pathname
556 :element-type '(unsigned-byte 8)
557 :if-does-not-exist nil)
558 (unless stream
559 (xslt-error "cannot find included stylesheet ~A at ~A"
560 uri pathname))
561 (when (find str *xsl-include-stack* :test #'equal)
562 (xslt-error "recursive inclusion of ~A" uri))
563 (let* ((*xsl-include-stack* (cons str *xsl-include-stack*))
564 (<transform>2 (parse-stylesheet-to-stp stream uri-resolver)))
565 (stp:insert-child-after <transform>
566 (stp:copy <transform>2)
567 include)
568 (stp:detach include)))))
569 <transform>))
571 (defvar *instruction-base-uri*) ;misnamed, is also used in other attributes
572 (defvar *apply-imports-limit*)
573 (defvar *import-priority*)
574 (defvar *extension-namespaces*)
575 (defvar *forwards-compatible-p*)
577 (defmacro do-toplevel ((var xpath <transform>) &body body)
578 `(map-toplevel (lambda (,var) ,@body) ,xpath ,<transform>))
580 (defun map-toplevel (fn xpath <transform>)
581 (dolist (node (list-toplevel xpath <transform>))
582 (let ((*namespaces* *initial-namespaces*))
583 (xpath:do-node-set (ancestor (xpath:evaluate "ancestor::node()" node))
584 (xpath:with-namespaces (("" #.*xsl*))
585 (when (xpath:node-matches-p ancestor "stylesheet|transform")
586 ;; discard namespaces from including stylesheets
587 (setf *namespaces* *initial-namespaces*)))
588 (when (xpath-protocol:node-type-p ancestor :element)
589 (setf *namespaces* (acons-namespaces ancestor *namespaces* t))))
590 (funcall fn node))))
592 (defun list-toplevel (xpath <transform>)
593 (labels ((recurse (sub)
594 (let ((subsubs
595 (xpath-sys:pipe-of
596 (xpath:evaluate "transform|stylesheet" sub))))
597 (xpath::append-pipes
598 (xpath-sys:pipe-of (xpath:evaluate xpath sub))
599 (xpath::mappend-pipe #'recurse subsubs)))))
600 (xpath::sort-nodes (recurse <transform>))))
602 (defmacro with-import-magic ((node env) &body body)
603 `(invoke-with-import-magic (lambda () ,@body) ,node ,env))
605 (defun invoke-with-import-magic (fn node env)
606 (unless (or (namep node "stylesheet") (namep node "transform"))
607 (setf node (stp:parent node)))
608 (let ((*excluded-namespaces* (list *xsl*))
609 (*extension-namespaces* '())
610 (*forwards-compatible-p*
611 (not (equal (stp:attribute-value node "version") "1.0"))))
612 (parse-exclude-result-prefixes! node env)
613 (parse-extension-element-prefixes! node env)
614 (funcall fn)))
616 (defun parse-1-stylesheet (env stylesheet designator uri-resolver)
617 (let* ((<transform> (parse-stylesheet-to-stp designator uri-resolver))
618 (instruction-base-uri (stp:base-uri <transform>))
619 (namespaces (acons-namespaces <transform>))
620 (apply-imports-limit (1+ *import-priority*))
621 (continuations '()))
622 (let ((*namespaces* namespaces))
623 (invoke-with-import-magic (constantly t) <transform> env))
624 (do-toplevel (elt "node()" <transform>)
625 (let ((version (stp:attribute-value (stp:parent elt) "version")))
626 (cond
627 ((null version)
628 (xslt-error "stylesheet lacks version"))
629 ((equal version "1.0")
630 (if (typep elt 'stp:element)
631 (when (or (equal (stp:namespace-uri elt) "")
632 (and (equal (stp:namespace-uri elt) *xsl*)
633 (not (find (stp:local-name elt)
634 '("key" "template" "output"
635 "strip-space" "preserve-space"
636 "attribute-set" "namespace-alias"
637 "decimal-format" "variable" "param"
638 "import" "include"
639 ;; for include handling:
640 "stylesheet" "transform")
641 :test #'equal))))
642 (xslt-error "unknown top-level element ~A" (stp:local-name elt)))
643 (xslt-error "text at top-level"))))))
644 (macrolet ((with-specials ((&optional) &body body)
645 `(let ((*instruction-base-uri* instruction-base-uri)
646 (*namespaces* namespaces)
647 (*apply-imports-limit* apply-imports-limit))
648 ,@body)))
649 (with-specials ()
650 (do-toplevel (import "import" <transform>)
651 (when (let ((prev (xpath:first-node
652 (xpath:evaluate "preceding-sibling::*"
653 import
654 t))))
655 (and prev (not (namep prev "import"))))
656 (xslt-error "import not at beginning of stylesheet"))
657 (let ((uri (puri:merge-uris (or (stp:attribute-value import "href")
658 (xslt-error "import without href"))
659 (stp:base-uri import))))
660 (push (parse-imported-stylesheet env stylesheet uri uri-resolver)
661 continuations))))
662 (let ((import-priority
663 (incf *import-priority*))
664 (var-cont (prepare-global-variables stylesheet <transform>)))
665 (parse-namespace-aliases! stylesheet <transform> env)
666 ;; delay the rest of compilation until we've seen all global
667 ;; variables:
668 (lambda ()
669 (mapc #'funcall (nreverse continuations))
670 (with-specials ()
671 (let ((*import-priority* import-priority))
672 (funcall var-cont)
673 (parse-keys! stylesheet <transform> env)
674 (parse-templates! stylesheet <transform> env)
675 (parse-output! stylesheet <transform> env)
676 (parse-strip/preserve-space! stylesheet <transform> env)
677 (parse-attribute-sets! stylesheet <transform> env)
678 (parse-decimal-formats! stylesheet <transform> env))))))))
680 (defvar *xsl-import-stack* nil)
682 (defun parse-imported-stylesheet (env stylesheet uri uri-resolver)
683 (let* ((uri (if uri-resolver
684 (funcall uri-resolver uri)
685 uri))
686 (str (puri:render-uri uri nil))
687 (pathname
688 (handler-case
689 (uri-to-pathname uri)
690 (cxml:xml-parse-error (c)
691 (xslt-error "cannot find imported stylesheet ~A: ~A"
692 uri c)))))
693 (with-open-file
694 (stream pathname
695 :element-type '(unsigned-byte 8)
696 :if-does-not-exist nil)
697 (unless stream
698 (xslt-error "cannot find imported stylesheet ~A at ~A"
699 uri pathname))
700 (when (find str *xsl-import-stack* :test #'equal)
701 (xslt-error "recursive inclusion of ~A" uri))
702 (let ((*xsl-import-stack* (cons str *xsl-import-stack*)))
703 (parse-1-stylesheet env stylesheet stream uri-resolver)))))
705 (defvar *included-attribute-sets*)
707 (defun parse-stylesheet (designator &key uri-resolver)
708 (with-resignalled-errors ()
709 (xpath:with-namespaces ((nil #.*xsl*))
710 (let* ((*import-priority* 0)
711 (xpath:*allow-variables-in-patterns* nil)
712 (puri:*strict-parse* nil)
713 (stylesheet (make-stylesheet))
714 (*stylesheet*
715 ;; zzz this is for remove-excluded-namespaces only
716 stylesheet)
717 (env (make-instance 'lexical-xslt-environment))
718 (*excluded-namespaces* *excluded-namespaces*)
719 (*global-variable-declarations* (make-empty-declaration-array))
720 (*included-attribute-sets* nil))
721 (ensure-mode stylesheet nil)
722 (funcall (parse-1-stylesheet env stylesheet designator uri-resolver))
723 ;; reverse attribute sets:
724 (let ((table (stylesheet-attribute-sets stylesheet)))
725 (maphash (lambda (k v)
726 (setf (gethash k table) (nreverse v)))
727 table))
728 ;; for Errors_err011
729 (dolist (sets *included-attribute-sets*)
730 (loop for (local-name uri nil) in sets do
731 (find-attribute-set local-name uri stylesheet)))
732 ;; add default df
733 (unless (find-decimal-format "" "" stylesheet nil)
734 (setf (find-decimal-format "" "" stylesheet)
735 (make-decimal-format)))
736 ;; compile a template matcher for each mode:
737 (loop
738 for mode being each hash-value in (stylesheet-modes stylesheet)
740 (setf (mode-match-thunk mode)
741 (xpath:make-pattern-matcher
742 (mapcar #'template-compiled-pattern
743 (mode-templates mode)))))
744 ;; and for the strip tests
745 (setf (stylesheet-strip-thunk stylesheet)
746 (let ((patterns (stylesheet-strip-tests stylesheet)))
747 (and patterns
748 (xpath:make-pattern-matcher
749 (mapcar #'strip-test-compiled-pattern patterns)))))
750 stylesheet))))
752 (defun parse-attribute-sets! (stylesheet <transform> env)
753 (do-toplevel (elt "attribute-set" <transform>)
754 (with-import-magic (elt env)
755 (push (let* ((sets
756 (mapcar (lambda (qname)
757 (multiple-value-list (decode-qname qname env nil)))
758 (words
759 (stp:attribute-value elt "use-attribute-sets"))))
760 (instructions
761 (stp:map-children
762 'list
763 (lambda (child)
764 (unless
765 (and (typep child 'stp:element)
766 (or (and (equal (stp:namespace-uri child) *xsl*)
767 (equal (stp:local-name child)
768 "attribute"))
769 (find (stp:namespace-uri child)
770 *extension-namespaces*
771 :test 'equal)))
772 (xslt-error "non-attribute found in attribute set"))
773 (parse-instruction child))
774 elt))
775 (*lexical-variable-declarations*
776 (make-empty-declaration-array))
777 (thunk
778 (compile-instruction `(progn ,@instructions) env))
779 (n-variables (length *lexical-variable-declarations*)))
780 (push sets *included-attribute-sets*)
781 (lambda (ctx)
782 (with-stack-limit ()
783 (loop for (local-name uri nil) in sets do
784 (dolist (thunk (find-attribute-set local-name uri))
785 (funcall thunk ctx)))
786 (let ((*lexical-variable-values*
787 (make-variable-value-array n-variables)))
788 (funcall thunk ctx)))))
789 (gethash (multiple-value-bind (local-name uri)
790 (decode-qname (or (stp:attribute-value elt "name")
791 (xslt-error "missing name"))
793 nil)
794 (cons local-name uri))
795 (stylesheet-attribute-sets stylesheet))))))
797 (defun parse-namespace-aliases! (stylesheet <transform> env)
798 (do-toplevel (elt "namespace-alias" <transform>)
799 (let ((*namespaces* (acons-namespaces elt)))
800 (only-with-attributes (stylesheet-prefix result-prefix) elt
801 (unless stylesheet-prefix
802 (xslt-error "missing stylesheet-prefix in namespace-alias"))
803 (unless result-prefix
804 (xslt-error "missing result-prefix in namespace-alias"))
805 (setf (gethash
806 (if (equal stylesheet-prefix "#default")
808 (or (xpath-sys:environment-find-namespace
810 stylesheet-prefix)
811 (xslt-error "stylesheet namespace not found in alias: ~A"
812 stylesheet-prefix)))
813 (stylesheet-namespace-aliases stylesheet))
814 (or (xpath-sys:environment-find-namespace
816 (if (equal result-prefix "#default")
818 result-prefix))
819 (xslt-error "result namespace not found in alias: ~A"
820 result-prefix)))))))
822 (defun parse-decimal-formats! (stylesheet <transform> env)
823 (do-toplevel (elt "decimal-format" <transform>)
824 (stp:with-attributes (name
825 ;; strings
826 infinity
827 (nan "NaN")
828 ;; characters:
829 decimal-separator
830 grouping-separator
831 zero-digit
832 percent
833 per-mille
834 digit
835 pattern-separator
836 minus-sign)
838 (multiple-value-bind (local-name uri)
839 (if name
840 (decode-qname name env nil)
841 (values "" ""))
842 (let ((current (find-decimal-format local-name uri stylesheet nil))
843 (new
844 (let ((seen '()))
845 (flet ((chr (key x)
846 (when x
847 (unless (eql (length x) 1)
848 (xslt-error "not a single character: ~A" x))
849 (let ((chr (elt x 0)))
850 (when (find chr seen)
851 (xslt-error
852 "conflicting decimal format characters: ~A"
853 chr))
854 (push chr seen)
855 (list key chr))))
856 (str (key x)
857 (when x
858 (list key x))))
859 (apply #'make-decimal-format
860 (append (str :infinity infinity)
861 (str :nan nan)
862 (chr :decimal-separator decimal-separator)
863 (chr :grouping-separator grouping-separator)
864 (chr :zero-digit zero-digit)
865 (chr :percent percent)
866 (chr :per-mille per-mille)
867 (chr :digit digit)
868 (chr :pattern-separator pattern-separator)
869 (chr :minus-sign minus-sign)))))))
870 (if current
871 (unless (decimal-format= current new)
872 (xslt-error "decimal format mismatch for ~S" local-name))
873 (setf (find-decimal-format local-name uri stylesheet) new)))))))
875 (defun parse-exclude-result-prefixes! (node env)
876 (stp:with-attributes (exclude-result-prefixes)
877 node
878 (dolist (prefix (words (or exclude-result-prefixes "")))
879 (if (equal prefix "#default")
880 (setf prefix nil)
881 (unless (cxml-stp-impl::nc-name-p prefix)
882 (xslt-error "invalid prefix: ~A" prefix)))
883 (push (or (xpath-sys:environment-find-namespace env prefix)
884 (xslt-error "namespace not found: ~A" prefix))
885 *excluded-namespaces*))))
887 (defun parse-extension-element-prefixes! (node env)
888 (stp:with-attributes (extension-element-prefixes)
889 node
890 (dolist (prefix (words (or extension-element-prefixes "")))
891 (if (equal prefix "#default")
892 (setf prefix nil)
893 (unless (cxml-stp-impl::nc-name-p prefix)
894 (xslt-error "invalid prefix: ~A" prefix)))
895 (let ((uri
896 (or (xpath-sys:environment-find-namespace env prefix)
897 (xslt-error "namespace not found: ~A" prefix))))
898 (unless (equal uri *xsl*)
899 (push uri *extension-namespaces*)
900 (push uri *excluded-namespaces*))))))
902 (defun parse-nametest-tokens (str)
903 (labels ((check (boolean)
904 (unless boolean
905 (xslt-error "invalid nametest token")))
906 (check-null (boolean)
907 (check (not boolean))))
908 (cons
909 :patterns
910 (mapcar (lambda (name-test)
911 (destructuring-bind (&optional path &rest junk)
912 (cdr (xpath:parse-pattern-expression name-test))
913 (check-null junk)
914 (check (eq (car path) :path))
915 (destructuring-bind (&optional child &rest junk) (cdr path)
916 (check-null junk)
917 (check (eq (car child) :child))
918 (destructuring-bind (nodetest &rest junk) (cdr child)
919 (check-null junk)
920 (check (or (stringp nodetest)
921 (eq nodetest '*)
922 (and (consp nodetest)
923 (or (eq (car nodetest) :namespace)
924 (eq (car nodetest) :qname)))))))
925 path))
926 (words str)))))
928 (defstruct strip-test
929 compiled-pattern
930 priority
931 position
932 value)
934 (defun parse-strip/preserve-space! (stylesheet <transform> env)
935 (let ((i 0))
936 (do-toplevel (elt "strip-space|preserve-space" <transform>)
937 (let ((*namespaces* (acons-namespaces elt))
938 (value
939 (if (equal (stp:local-name elt) "strip-space")
940 :strip
941 :preserve)))
942 (dolist (expression
943 (cdr (parse-nametest-tokens
944 (stp:attribute-value elt "elements"))))
945 (let* ((compiled-pattern
946 (car (without-xslt-current ()
947 (xpath:compute-patterns
948 `(:patterns ,expression)
949 *import-priority*
950 "will set below"
951 env))))
952 (strip-test
953 (make-strip-test :compiled-pattern compiled-pattern
954 :priority (expression-priority expression)
955 :position i
956 :value value)))
957 (setf (xpath:pattern-value compiled-pattern) strip-test)
958 (push strip-test (stylesheet-strip-tests stylesheet)))))
959 (incf i))))
961 (defstruct (output-specification
962 (:conc-name "OUTPUT-"))
963 method
964 indent
965 omit-xml-declaration
966 encoding
967 doctype-system
968 doctype-public
969 cdata-section-matchers)
971 (defun parse-output! (stylesheet <transform> env)
972 (dolist (<output> (list-toplevel "output" <transform>))
973 (let ((spec (stylesheet-output-specification stylesheet)))
974 (only-with-attributes (version
975 method
976 indent
977 encoding
978 media-type
979 doctype-system
980 doctype-public
981 omit-xml-declaration
982 standalone
983 cdata-section-elements)
984 <output>
985 (declare (ignore version
986 ;; FIXME:
987 media-type
988 standalone))
989 (when method
990 (setf (output-method spec) method))
991 (when indent
992 (setf (output-indent spec) indent))
993 (when encoding
994 (setf (output-encoding spec) encoding))
995 (when doctype-system
996 (setf (output-doctype-system spec) doctype-system))
997 (when doctype-public
998 (setf (output-doctype-public spec) doctype-public))
999 (when omit-xml-declaration
1000 (setf (output-omit-xml-declaration spec) omit-xml-declaration))
1001 (when cdata-section-elements
1002 (dolist (qname (words cdata-section-elements))
1003 (decode-qname qname env nil) ;check the syntax
1004 (push (xpath:make-pattern-matcher* qname env)
1005 (output-cdata-section-matchers spec))))))))
1007 (defun make-empty-declaration-array ()
1008 (make-array 1 :fill-pointer 0 :adjustable t))
1010 (defun make-variable-value-array (n-lexical-variables)
1011 (make-array n-lexical-variables :initial-element 'unbound))
1013 (defun compile-global-variable (<variable> env) ;; also for <param>
1014 (stp:with-attributes (name select) <variable>
1015 (when (and select (stp:list-children <variable>))
1016 (xslt-error "variable with select and body"))
1017 (let* ((*lexical-variable-declarations* (make-empty-declaration-array))
1018 (inner (cond
1019 (select
1020 (compile-xpath select env))
1021 ((stp:list-children <variable>)
1022 (let* ((inner-sexpr `(progn ,@(parse-body <variable>)))
1023 (inner-thunk (compile-instruction inner-sexpr env)))
1024 (lambda (ctx)
1025 (apply-to-result-tree-fragment ctx inner-thunk))))
1027 (lambda (ctx)
1028 (declare (ignore ctx))
1029 ""))))
1030 (n-lexical-variables (length *lexical-variable-declarations*)))
1031 (xslt-trace-thunk
1032 (lambda (ctx)
1033 (let* ((*lexical-variable-values*
1034 (make-variable-value-array n-lexical-variables)))
1035 (funcall inner ctx)))
1036 "global ~s (~s) = ~s" name select :result))))
1038 (defstruct (variable-chain
1039 (:constructor make-variable-chain)
1040 (:conc-name "VARIABLE-CHAIN-"))
1041 definitions
1042 index
1043 local-name
1044 thunk
1045 uri)
1047 (defstruct (import-variable
1048 (:constructor make-variable)
1049 (:conc-name "VARIABLE-"))
1050 value-thunk
1051 value-thunk-setter
1052 param-p)
1054 (defun parse-global-variable! (stylesheet <variable> global-env)
1055 (let* ((*namespaces* (acons-namespaces <variable>))
1056 (instruction-base-uri (stp:base-uri <variable>))
1057 (*instruction-base-uri* instruction-base-uri)
1058 (*excluded-namespaces* (list *xsl*))
1059 (*extension-namespaces* '())
1060 (qname (stp:attribute-value <variable> "name")))
1061 (with-import-magic (<variable> global-env)
1062 (unless qname
1063 (xslt-error "name missing in ~A" (stp:local-name <variable>)))
1064 (multiple-value-bind (local-name uri)
1065 (decode-qname qname global-env nil)
1066 ;; For the normal compilation environment of templates, install it
1067 ;; into *GLOBAL-VARIABLE-DECLARATIONS*:
1068 (let ((index (intern-global-variable local-name uri)))
1069 ;; For the evaluation of a global variable itself, build a thunk
1070 ;; that lazily resolves other variables, stored into
1071 ;; INITIAL-GLOBAL-VARIABLE-THUNKS:
1072 (let* ((value-thunk :unknown)
1073 (sgv (stylesheet-global-variables stylesheet))
1074 (chain
1075 (if (< index (length sgv))
1076 (elt sgv index)
1077 (make-variable-chain
1078 :index index
1079 :local-name local-name
1080 :uri uri)))
1081 (next (car (variable-chain-definitions chain)))
1082 (global-variable-thunk
1083 (lambda (ctx)
1084 (let ((v (global-variable-value index nil)))
1085 (cond
1086 ((eq v 'seen)
1087 (unless next
1088 (xslt-error "no next definition for: ~A"
1089 local-name))
1090 (funcall (variable-value-thunk next) ctx))
1091 ((eq v 'unbound)
1092 (setf (global-variable-value index) 'seen)
1093 (setf (global-variable-value index)
1094 (funcall value-thunk ctx)))
1096 v)))))
1097 (excluded-namespaces *excluded-namespaces*)
1098 (extension-namespaces *extension-namespaces*)
1099 (variable
1100 (make-variable :param-p (namep <variable> "param")))
1101 (forwards-compatible-p *forwards-compatible-p*)
1102 (value-thunk-setter
1103 (lambda ()
1104 (let* ((*instruction-base-uri* instruction-base-uri)
1105 (*excluded-namespaces* excluded-namespaces)
1106 (*extension-namespaces* extension-namespaces)
1107 (*forwards-compatible-p* forwards-compatible-p)
1109 (compile-global-variable <variable> global-env)))
1110 (setf value-thunk fn)
1111 (setf (variable-value-thunk variable) fn)))))
1112 (setf (variable-value-thunk-setter variable)
1113 value-thunk-setter)
1114 (setf (gethash (cons local-name uri)
1115 (initial-global-variable-thunks global-env))
1116 global-variable-thunk)
1117 (setf (variable-chain-thunk chain) global-variable-thunk)
1118 (push variable (variable-chain-definitions chain))
1119 chain))))))
1121 (defun parse-keys! (stylesheet <transform> env)
1122 (xpath:with-namespaces ((nil #.*xsl*))
1123 (do-toplevel (<key> "key" <transform>)
1124 (with-import-magic (<key> env)
1125 (let ((*instruction-base-uri* (stp:base-uri <key>)))
1126 (only-with-attributes (name match use) <key>
1127 (unless name (xslt-error "key name attribute not specified"))
1128 (unless match (xslt-error "key match attribute not specified"))
1129 (unless use (xslt-error "key use attribute not specified"))
1130 (multiple-value-bind (local-name uri)
1131 (decode-qname name env nil)
1132 (add-key stylesheet
1133 (cons local-name uri)
1134 (compile-xpath `(xpath:xpath ,(parse-key-pattern match))
1135 env)
1136 (compile-xpath use
1137 (make-instance 'key-environment))))))))))
1139 (defun prepare-global-variables (stylesheet <transform>)
1140 (xpath:with-namespaces ((nil #.*xsl*))
1141 (let* ((igvt (stylesheet-initial-global-variable-thunks stylesheet))
1142 (global-env (make-instance 'global-variable-environment
1143 :initial-global-variable-thunks igvt))
1144 (chains '()))
1145 (do-toplevel (<variable> "variable|param" <transform>)
1146 (let ((chain
1147 (parse-global-variable! stylesheet <variable> global-env)))
1148 (xslt-trace "parsing global variable ~s (uri ~s)"
1149 (variable-chain-local-name chain)
1150 (variable-chain-uri chain))
1151 (when (find chain
1152 chains
1153 :test (lambda (a b)
1154 (and (equal (variable-chain-local-name a)
1155 (variable-chain-local-name b))
1156 (equal (variable-chain-uri a)
1157 (variable-chain-uri b)))))
1158 (xslt-error "duplicate definition for global variable ~A"
1159 (variable-chain-local-name chain)))
1160 (push chain chains)))
1161 (setf chains (nreverse chains))
1162 (let ((table (stylesheet-global-variables stylesheet))
1163 (newlen (length *global-variable-declarations*)))
1164 (adjust-array table newlen :fill-pointer newlen)
1165 (dolist (chain chains)
1166 (setf (elt table (variable-chain-index chain)) chain)))
1167 (lambda ()
1168 ;; now that the global environment knows about all variables, run the
1169 ;; thunk setters to perform their compilation
1170 (mapc (lambda (chain)
1171 (dolist (var (variable-chain-definitions chain))
1172 (funcall (variable-value-thunk-setter var))))
1173 chains)))))
1175 (defun parse-templates! (stylesheet <transform> env)
1176 (let ((i 0))
1177 (do-toplevel (<template> "template" <transform>)
1178 (let ((*namespaces* (acons-namespaces <template>))
1179 (*instruction-base-uri* (stp:base-uri <template>)))
1180 (with-import-magic (<template> env)
1181 (dolist (template (compile-template <template> env i))
1182 (let ((name (template-name template)))
1183 (if name
1184 (let* ((table (stylesheet-named-templates stylesheet))
1185 (head (car (gethash name table))))
1186 (when (and head (eql (template-import-priority head)
1187 (template-import-priority template)))
1188 ;; fixme: is this supposed to be a run-time error?
1189 (xslt-error "conflicting templates for ~A" name))
1190 (push template (gethash name table)))
1191 (let ((mode (ensure-mode/qname stylesheet
1192 (template-mode-qname template)
1193 env)))
1194 (setf (template-mode template) mode)
1195 (push template (mode-templates mode))))))))
1196 (incf i))))
1199 ;;;; APPLY-STYLESHEET
1201 (defvar *stylesheet*)
1203 (deftype xml-designator () '(or runes:xstream runes:rod array stream pathname))
1205 (defun unalias-uri (uri)
1206 (let ((result
1207 (gethash uri (stylesheet-namespace-aliases *stylesheet*)
1208 uri)))
1209 (check-type result string)
1210 result))
1212 (defstruct (parameter
1213 (:constructor make-parameter (value local-name &optional uri)))
1214 (uri "")
1215 local-name
1216 value)
1218 (defun find-parameter-value (local-name uri parameters)
1219 (dolist (p parameters)
1220 (when (and (equal (parameter-local-name p) local-name)
1221 (equal (parameter-uri p) uri))
1222 (return (parameter-value p)))))
1224 (defvar *uri-resolver*)
1226 (defun parse-allowing-microsoft-bom (pathname handler)
1227 (with-open-file (s pathname :element-type '(unsigned-byte 8))
1228 (unless (and (eql (read-byte s nil) #xef)
1229 (eql (read-byte s nil) #xbb)
1230 (eql (read-byte s nil) #xbf))
1231 (file-position s 0))
1232 (cxml:parse s handler)))
1234 (defvar *documents*)
1236 (defun %document (uri-string base-uri)
1237 (let* ((absolute-uri
1238 (puri:merge-uris uri-string (or base-uri "")))
1239 (resolved-uri
1240 (if *uri-resolver*
1241 (funcall *uri-resolver* absolute-uri)
1242 absolute-uri))
1243 (pathname
1244 (handler-case
1245 (uri-to-pathname resolved-uri)
1246 (cxml:xml-parse-error (c)
1247 (xslt-error "cannot find referenced document ~A: ~A"
1248 resolved-uri c))))
1249 (xpath-root-node
1250 (or (gethash pathname *documents*)
1251 (setf (gethash pathname *documents*)
1252 (make-whitespace-stripper
1253 (handler-case
1254 (parse-allowing-microsoft-bom pathname
1255 (stp:make-builder))
1256 ((or file-error cxml:xml-parse-error) (c)
1257 (xslt-error "cannot parse referenced document ~A: ~A"
1258 pathname c)))
1259 (stylesheet-strip-thunk *stylesheet*))))))
1260 (when (puri:uri-fragment absolute-uri)
1261 (xslt-error "use of fragment identifiers in document() not supported"))
1262 xpath-root-node))
1264 (xpath-sys:define-extension xslt *xsl*)
1266 (defun document-base-uri (node)
1267 (xpath-protocol:base-uri
1268 (cond
1269 ((xpath-protocol:node-type-p node :document)
1270 (xpath::find-in-pipe-if
1271 (lambda (x)
1272 (xpath-protocol:node-type-p x :element))
1273 (xpath-protocol:child-pipe node)))
1274 ((xpath-protocol:node-type-p node :element)
1275 node)
1277 (xpath-protocol:parent-node node)))))
1279 (xpath-sys:define-xpath-function/lazy
1280 xslt :document
1281 (object &optional node-set)
1282 (let ((instruction-base-uri *instruction-base-uri*))
1283 (lambda (ctx)
1284 (let* ((object (funcall object ctx))
1285 (node-set (and node-set (funcall node-set ctx)))
1286 (base-uri
1287 (if node-set
1288 (document-base-uri (xpath::textually-first-node node-set))
1289 instruction-base-uri)))
1290 (xpath-sys:make-node-set
1291 (if (xpath:node-set-p object)
1292 (xpath:map-node-set->list
1293 (lambda (node)
1294 (%document (xpath:string-value node)
1295 (if node-set
1296 base-uri
1297 (document-base-uri node))))
1298 object)
1299 (list (%document (xpath:string-value object) base-uri))))))))
1301 ;; FIXME: the point of keys is that we are meant to optimize this
1302 ;; by building a table mapping nodes to values for each key.
1303 ;; We should run over all matching nodes and store them in such a table
1304 ;; when seeing their document for the first time.
1305 (xpath-sys:define-xpath-function/lazy xslt :key (name object)
1306 (let ((namespaces *namespaces*))
1307 (lambda (ctx)
1308 (let* ((qname (xpath:string-value (funcall name ctx)))
1309 (object (funcall object ctx))
1310 (expanded-name
1311 (multiple-value-bind (local-name uri)
1312 (decode-qname/runtime qname namespaces nil)
1313 (cons local-name uri)))
1314 (key-conses (find-key expanded-name *stylesheet*)))
1315 (xpath-sys:make-node-set
1316 (xpath::mappend-pipe
1317 (lambda (key)
1318 (labels ((get-by-key (value)
1319 (let ((value (xpath:string-value value)))
1320 (xpath::filter-pipe
1321 #'(lambda (node)
1322 (let ((uses
1323 (xpath:evaluate-compiled (key-use key)
1324 node)))
1325 (if (xpath:node-set-p uses)
1326 (xpath::find-in-pipe
1327 value
1328 (xpath-sys:pipe-of uses)
1329 :key #'xpath:string-value
1330 :test #'equal)
1331 (equal value (xpath:string-value uses)))))
1332 (xpath-sys:pipe-of
1333 (xpath:node-set-value
1334 (xpath:evaluate-compiled (key-match key) ctx)))))))
1335 (xpath::sort-pipe
1336 (if (xpath:node-set-p object)
1337 (xpath::mappend-pipe #'get-by-key
1338 (xpath-sys:pipe-of object))
1339 (get-by-key object)))))
1340 key-conses))))))
1342 ;; FIXME: add alias mechanism for XPath extensions in order to avoid duplication
1344 (xpath-sys:define-xpath-function/lazy xslt :current ()
1345 (when *without-xslt-current-p*
1346 (xslt-error "current() not allowed here"))
1347 #'(lambda (ctx)
1348 (xpath-sys:make-node-set
1349 (xpath-sys:make-pipe
1350 (xpath:context-starting-node ctx)
1351 nil))))
1353 (xpath-sys:define-xpath-function/lazy xslt :unparsed-entity-uri (name)
1354 #'(lambda (ctx)
1355 (or (xpath-protocol:unparsed-entity-uri (xpath:context-node ctx)
1356 (funcall name ctx))
1357 "")))
1359 (defun %get-node-id (node)
1360 (when (xpath:node-set-p node)
1361 (setf node (xpath::textually-first-node node)))
1362 (when node
1363 (let ((id (xpath-sys:get-node-id node))
1364 (highest-base-uri
1365 (loop
1366 for parent = node then next
1367 for next = (xpath-protocol:parent-node parent)
1368 for this-base-uri = (xpath-protocol:base-uri parent)
1369 for highest-base-uri = (if (plusp (length this-base-uri))
1370 this-base-uri
1371 highest-base-uri)
1372 while next
1373 finally (return highest-base-uri))))
1374 ;; FIXME: Now that we intern documents, we could use a short ID for
1375 ;; the document instead of the base URI.
1376 (nreverse (concatenate 'string highest-base-uri "//" id)))))
1378 (xpath-sys:define-xpath-function/lazy xslt :generate-id (&optional node-set-thunk)
1379 (if node-set-thunk
1380 #'(lambda (ctx)
1381 (%get-node-id (xpath:node-set-value (funcall node-set-thunk ctx))))
1382 #'(lambda (ctx)
1383 (%get-node-id (xpath:context-node ctx)))))
1385 (declaim (special *available-instructions*))
1387 (xpath-sys:define-xpath-function/lazy xslt :element-available (qname)
1388 (let ((namespaces *namespaces*))
1389 #'(lambda (ctx)
1390 (let ((qname (funcall qname ctx)))
1391 (multiple-value-bind (local-name uri)
1392 (decode-qname/runtime qname namespaces nil)
1393 (and (equal uri *xsl*)
1394 (gethash local-name *available-instructions*)
1395 t))))))
1397 (xpath-sys:define-xpath-function/lazy xslt :function-available (qname)
1398 (let ((namespaces *namespaces*))
1399 #'(lambda (ctx)
1400 (let ((qname (funcall qname ctx)))
1401 (multiple-value-bind (local-name uri)
1402 (decode-qname/runtime qname namespaces nil)
1403 (and (zerop (length uri))
1404 (or (xpath-sys:find-xpath-function local-name *xsl*)
1405 (xpath-sys:find-xpath-function local-name uri))
1406 t))))))
1408 (xpath-sys:define-xpath-function/lazy xslt :system-property (qname)
1409 (let ((namespaces *namespaces*))
1410 (lambda (ctx)
1411 (let ((qname (xpath:string-value (funcall qname ctx))))
1412 (multiple-value-bind (local-name uri)
1413 (decode-qname/runtime qname namespaces nil)
1414 (if (equal uri *xsl*)
1415 (cond
1416 ((equal local-name "version")
1417 "1")
1418 ((equal local-name "vendor")
1419 "Xuriella")
1420 ((equal local-name "vendor-uri")
1421 "http://repo.or.cz/w/xuriella.git")
1423 ""))
1424 ""))))))
1426 ;; FIXME: should there be separate uri-resolver arguments for stylesheet
1427 ;; and data?
1428 (defun apply-stylesheet
1429 (stylesheet source-designator
1430 &key output parameters uri-resolver navigator)
1431 (when (typep stylesheet 'xml-designator)
1432 (setf stylesheet
1433 (handler-bind
1434 ((cxml:xml-parse-error
1435 (lambda (c)
1436 (xslt-error "cannot parse stylesheet: ~A" c))))
1437 (parse-stylesheet stylesheet :uri-resolver uri-resolver))))
1438 (with-resignalled-errors ()
1439 (invoke-with-output-sink
1440 (lambda ()
1441 (let* ((*documents* (make-hash-table :test 'equal))
1442 (xpath:*navigator* (or navigator :default-navigator))
1443 (puri:*strict-parse* nil)
1444 (*stylesheet* stylesheet)
1445 (*empty-mode* (make-mode))
1446 (*default-mode* (find-mode stylesheet nil))
1447 (global-variable-chains
1448 (stylesheet-global-variables stylesheet))
1449 (*global-variable-values*
1450 (make-variable-value-array (length global-variable-chains)))
1451 (*uri-resolver* uri-resolver)
1452 (source-document
1453 (if (typep source-designator 'xml-designator)
1454 (cxml:parse source-designator (stp:make-builder))
1455 source-designator))
1456 (xpath-root-node
1457 (make-whitespace-stripper
1458 source-document
1459 (stylesheet-strip-thunk stylesheet)))
1460 (ctx (xpath:make-context xpath-root-node)))
1461 (when (pathnamep source-designator)
1462 (setf (gethash source-designator *documents*) xpath-root-node))
1463 (map nil
1464 (lambda (chain)
1465 (let ((head (car (variable-chain-definitions chain))))
1466 (when (variable-param-p head)
1467 (let ((value
1468 (find-parameter-value
1469 (variable-chain-local-name chain)
1470 (variable-chain-uri chain)
1471 parameters)))
1472 (when value
1473 (setf (global-variable-value
1474 (variable-chain-index chain))
1475 value))))))
1476 global-variable-chains)
1477 (map nil
1478 (lambda (chain)
1479 (funcall (variable-chain-thunk chain) ctx))
1480 global-variable-chains)
1481 ;; zzz we wouldn't have to mask float traps here if we used the
1482 ;; XPath API properly. Unfortunately I've been using FUNCALL
1483 ;; everywhere instead of EVALUATE, so let's paper over that
1484 ;; at a central place to be sure:
1485 (xpath::with-float-traps-masked ()
1486 (apply-templates ctx :mode *default-mode*))))
1487 (stylesheet-output-specification stylesheet)
1488 output)))
1490 (defun find-attribute-set (local-name uri &optional (stylesheet *stylesheet*))
1491 (or (gethash (cons local-name uri) (stylesheet-attribute-sets stylesheet))
1492 (xslt-error "no such attribute set: ~A/~A" local-name uri)))
1494 (defun apply-templates/list (list &key param-bindings sort-predicate mode)
1495 (when sort-predicate
1496 (setf list
1497 (mapcar #'xpath:context-node
1498 (stable-sort (contextify-node-list list)
1499 sort-predicate))))
1500 (let* ((n (length list))
1501 (s/d (lambda () n)))
1502 (loop
1503 for i from 1
1504 for child in list
1506 (apply-templates (xpath:make-context child s/d i)
1507 :param-bindings param-bindings
1508 :mode mode))))
1510 (defvar *stack-limit* 200)
1512 (defun invoke-with-stack-limit (fn)
1513 (let ((*stack-limit* (1- *stack-limit*)))
1514 (unless (plusp *stack-limit*)
1515 (xslt-error "*stack-limit* reached; stack overflow"))
1516 (funcall fn)))
1518 (defun invoke-template (ctx template param-bindings)
1519 (let ((*lexical-variable-values*
1520 (make-variable-value-array (template-n-variables template))))
1521 (with-stack-limit ()
1522 (loop
1523 for (name-cons value) in param-bindings
1524 for (nil index nil) = (find name-cons
1525 (template-params template)
1526 :test #'equal
1527 :key #'car)
1529 (when index
1530 (setf (lexical-variable-value index) value)))
1531 (funcall (template-body template) ctx))))
1533 (defun apply-default-templates (ctx mode)
1534 (let ((node (xpath:context-node ctx)))
1535 (cond
1536 ((or (xpath-protocol:node-type-p node :processing-instruction)
1537 (xpath-protocol:node-type-p node :comment)))
1538 ((or (xpath-protocol:node-type-p node :text)
1539 (xpath-protocol:node-type-p node :attribute))
1540 (write-text (xpath-protocol:node-text node)))
1542 (apply-templates/list
1543 (xpath::force
1544 (xpath-protocol:child-pipe node))
1545 :mode mode)))))
1547 (defvar *apply-imports*)
1549 (defun apply-applicable-templates (ctx templates param-bindings finally)
1550 (labels ((apply-imports (&optional actual-param-bindings)
1551 (if templates
1552 (let* ((this (pop templates))
1553 (low (template-apply-imports-limit this))
1554 (high (template-import-priority this)))
1555 (setf templates
1556 (remove-if-not
1557 (lambda (x)
1558 (<= low (template-import-priority x) high))
1559 templates))
1560 (invoke-template ctx this actual-param-bindings))
1561 (funcall finally))))
1562 (let ((*apply-imports* #'apply-imports))
1563 (apply-imports param-bindings))))
1565 (defun apply-templates (ctx &key param-bindings mode)
1566 (apply-applicable-templates ctx
1567 (find-templates ctx (or mode *default-mode*))
1568 param-bindings
1569 (lambda ()
1570 (apply-default-templates ctx mode))))
1572 (defun call-template (ctx name &optional param-bindings)
1573 (apply-applicable-templates ctx
1574 (find-named-templates name)
1575 param-bindings
1576 (lambda ()
1577 (xslt-error "cannot find named template: ~s"
1578 name))))
1580 (defun find-templates (ctx mode)
1581 (let* ((matching-candidates
1582 (xpath:matching-values (mode-match-thunk mode)
1583 (xpath:context-node ctx)))
1584 (npriorities
1585 (if matching-candidates
1586 (1+ (reduce #'max
1587 matching-candidates
1588 :key #'template-import-priority))
1590 (priority-groups (make-array npriorities :initial-element nil)))
1591 (dolist (template matching-candidates)
1592 (push template
1593 (elt priority-groups (template-import-priority template))))
1594 (loop
1595 for i from (1- npriorities) downto 0
1596 for group = (elt priority-groups i)
1597 for template = (maximize #'template< group)
1598 when template
1599 collect template)))
1601 (defun find-named-templates (name)
1602 (gethash name (stylesheet-named-templates *stylesheet*)))
1604 (defun template< (a b) ;assuming same import priority
1605 (let ((p (template-priority a))
1606 (q (template-priority b)))
1607 (cond
1608 ((< p q) t)
1609 ((> p q) nil)
1611 (xslt-cerror "conflicting templates:~_~A,~_~A"
1612 (template-match-expression a)
1613 (template-match-expression b))
1614 (< (template-position a) (template-position b))))))
1616 (defun maximize (< things)
1617 (when things
1618 (let ((max (car things)))
1619 (dolist (other (cdr things))
1620 (when (funcall < max other)
1621 (setf max other)))
1622 max)))
1624 (defun invoke-with-output-sink (fn output-spec output)
1625 (etypecase output
1626 (pathname
1627 (with-open-file (s output
1628 :direction :output
1629 :element-type '(unsigned-byte 8)
1630 :if-exists :rename-and-delete)
1631 (invoke-with-output-sink fn output-spec s)))
1632 ((or stream null)
1633 (invoke-with-output-sink fn
1634 output-spec
1635 (make-output-sink output-spec output)))
1636 ((or hax:abstract-handler sax:abstract-handler)
1637 (with-xml-output output
1638 (when (typep output '(or combi-sink auto-detect-sink))
1639 (sax:start-dtd output
1640 :autodetect-me-please
1641 (output-doctype-public output-spec)
1642 (output-doctype-system output-spec)))
1643 (funcall fn)))))
1645 (defun make-output-sink (output-spec stream)
1646 (let* ((ystream
1647 (if stream
1648 (let ((et (stream-element-type stream)))
1649 (cond
1650 ((or (null et) (subtypep et '(unsigned-byte 8)))
1651 (runes:make-octet-stream-ystream stream))
1652 ((subtypep et 'character)
1653 (runes:make-character-stream-ystream stream))))
1654 (runes:make-rod-ystream)))
1655 (omit-xml-declaration-p
1656 (boolean-or-error (output-omit-xml-declaration output-spec)))
1657 (sink-encoding (or (output-encoding output-spec) "UTF-8"))
1658 (sax-target
1659 (progn
1660 (setf (runes:ystream-encoding ystream)
1661 (cxml::find-output-encoding sink-encoding))
1662 (make-instance 'cxml::sink
1663 :ystream ystream
1664 :omit-xml-declaration-p omit-xml-declaration-p
1665 :encoding sink-encoding))))
1666 (flet ((make-combi-sink ()
1667 (make-instance 'combi-sink
1668 :hax-target (make-instance 'chtml::sink
1669 :ystream ystream)
1670 :sax-target sax-target
1671 :encoding sink-encoding)))
1672 (let ((method-key
1673 (cond
1674 ((equalp (output-method output-spec) "HTML") :html)
1675 ((equalp (output-method output-spec) "TEXT") :text)
1676 ((equalp (output-method output-spec) "XML") :xml)
1677 (t nil))))
1678 (cond
1679 ((and (eq method-key :html)
1680 (null (output-doctype-system output-spec))
1681 (null (output-doctype-public output-spec)))
1682 (make-combi-sink))
1683 ((eq method-key :text)
1684 (make-text-filter sax-target))
1685 ((and (eq method-key :xml)
1686 (null (output-doctype-system output-spec)))
1687 sax-target)
1689 (make-auto-detect-sink (make-combi-sink) method-key)))))))
1691 (defstruct template
1692 match-expression
1693 compiled-pattern
1694 name
1695 import-priority
1696 apply-imports-limit
1697 priority
1698 position
1699 mode
1700 mode-qname
1701 params
1702 body
1703 n-variables)
1705 (defun expression-priority (form)
1706 (let ((step (second form)))
1707 (if (and (null (cddr form))
1708 (listp step)
1709 (member (car step) '(:child :attribute))
1710 (null (cddr step)))
1711 (let ((name (second step)))
1712 (cond
1713 ((or (stringp name)
1714 (and (consp name)
1715 (or (eq (car name) :qname)
1716 (eq (car name) :processing-instruction))))
1717 0.0)
1718 ((and (consp name)
1719 (or (eq (car name) :namespace)
1720 (eq (car name) '*)))
1721 -0.25)
1723 -0.5)))
1724 0.5)))
1726 (defun parse-key-pattern (str)
1727 (with-resignalled-errors ()
1728 (with-forward-compatible-errors
1729 (xpath:parse-xpath "compile-time-error()") ;hack
1730 (let ((parsed
1731 (mapcar #'(lambda (item)
1732 `(:path (:root :node)
1733 (:descendant-or-self *)
1734 ,@(cdr item)))
1735 (cdr (xpath::parse-pattern-expression str)))))
1736 (if (null (rest parsed))
1737 (first parsed)
1738 `(:union ,@parsed))))))
1740 (defun compile-value-thunk (value env)
1741 (if (and (listp value) (eq (car value) 'progn))
1742 (let ((inner-thunk (compile-instruction value env)))
1743 (lambda (ctx)
1744 (apply-to-result-tree-fragment ctx inner-thunk)))
1745 (compile-xpath value env)))
1747 (defun compile-var-binding (name value env)
1748 (multiple-value-bind (local-name uri)
1749 (decode-qname name env nil)
1750 (let ((thunk (xslt-trace-thunk
1751 (compile-value-thunk value env)
1752 "local variable ~s = ~s" name :result)))
1753 (list (cons local-name uri)
1754 (push-variable local-name
1756 *lexical-variable-declarations*)
1757 thunk))))
1759 (defun compile-var-bindings (forms env)
1760 (loop
1761 for (name value) in forms
1762 collect (compile-var-binding name value env)))
1764 (defun compile-template (<template> env position)
1765 (stp:with-attributes (match name priority mode) <template>
1766 (unless (or name match)
1767 (xslt-error "missing match in template"))
1768 (multiple-value-bind (params body-pos)
1769 (loop
1770 for i from 0
1771 for child in (stp:list-children <template>)
1772 while (namep child "param")
1773 collect (parse-param child) into params
1774 finally (return (values params i)))
1775 (let* ((*lexical-variable-declarations* (make-empty-declaration-array))
1776 (param-bindings (compile-var-bindings params env))
1777 (body (parse-body <template> body-pos (mapcar #'car params)))
1778 (body-thunk (compile-instruction `(progn ,@body) env))
1779 (outer-body-thunk
1780 (xslt-trace-thunk
1781 #'(lambda (ctx)
1782 (unwind-protect
1783 (progn
1784 ;; set params that weren't initialized by apply-templates
1785 (loop for (name index param-thunk) in param-bindings
1786 when (eq (lexical-variable-value index nil) 'unbound)
1787 do (setf (lexical-variable-value index)
1788 (funcall param-thunk ctx)))
1789 (funcall body-thunk ctx))))
1790 "template: match = ~s name = ~s" match name))
1791 (n-variables (length *lexical-variable-declarations*)))
1792 (append
1793 (when name
1794 (multiple-value-bind (local-name uri)
1795 (decode-qname name env nil)
1796 (list
1797 (make-template :name (cons local-name uri)
1798 :import-priority *import-priority*
1799 :apply-imports-limit *apply-imports-limit*
1800 :params param-bindings
1801 :body outer-body-thunk
1802 :n-variables n-variables))))
1803 (when match
1804 (mapcar (lambda (expression)
1805 (let* ((compiled-pattern
1806 (xslt-trace-thunk
1807 (car (without-xslt-current ()
1808 (xpath:compute-patterns
1809 `(:patterns ,expression)
1811 :dummy
1812 env)))
1813 "match-thunk for template (match ~s): ~s --> ~s"
1814 match expression :result))
1815 (p (if priority
1816 (xpath::parse-xnum priority)
1817 (expression-priority expression)))
1819 (progn
1820 (unless (and (numberp p)
1821 (not (xpath::inf-p p))
1822 (not (xpath::nan-p p)))
1823 (xslt-error "failed to parse priority"))
1824 (float p 1.0d0)))
1825 (template
1826 (make-template :match-expression expression
1827 :compiled-pattern compiled-pattern
1828 :import-priority *import-priority*
1829 :apply-imports-limit *apply-imports-limit*
1830 :priority p
1831 :position position
1832 :mode-qname mode
1833 :params param-bindings
1834 :body outer-body-thunk
1835 :n-variables n-variables)))
1836 (setf (xpath:pattern-value compiled-pattern)
1837 template)
1838 template))
1839 (cdr (xpath:parse-pattern-expression match)))))))))
1840 #+(or)
1841 (xuriella::parse-stylesheet #p"/home/david/src/lisp/xuriella/test.xsl")