Skip to content

Instantly share code, notes, and snippets.

@ruliana
Created June 18, 2023 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruliana/9c838ec1d7a34091378e49ee14542d56 to your computer and use it in GitHub Desktop.
Save ruliana/9c838ec1d7a34091378e49ee14542d56 to your computer and use it in GitHub Desktop.
XSLT Pseudo Implementation in Elisp
(defun apply-templates (node templates)
(let (output)
;; loop through each template
(dolist (template templates output)
;; if the current node matches the template
(when (matches node (car template))
;; apply the template and stop processing further templates
(setq output (apply-template node template))
(return output)))
;; if no template matched, process the node's children
(dolist (child (cdr node) output)
(setq output (apply-templates child templates)))))
(defun apply-template (node template)
(let (output)
;; loop through each instruction in our template
(dolist (instruction (cdr template) output)
;; if the instruction is to output text
(when (eq (car instruction) 'text)
;; add the instruction's text to our output
(setq output (concat output (cdr instruction))))
;; else if the instruction is to output the value of an XPath expression
(when (eq (car instruction) 'value-of)
;; evaluate the XPath expression and add its value to our output
(setq output (concat output (evaluate-xpath (cdr instruction) node))))
;; else if the instruction is to apply templates to an XPath expression
(when (eq (car instruction) 'apply-templates)
;; evaluate the XPath expression to get a set of nodes
(let ((nodes (evaluate-xpath (cdr instruction) node)))
;; apply templates to each node and add the results to our output
(dolist (subnode nodes output)
(setq output (concat output (apply-templates subnode templates)))))))))
(defun matches (node pattern)
;; this function would implement matching of the node against a template pattern
;; for simplicity, it's not implemented here
)
(defun evaluate-xpath (expression node)
;; this function would implement the evaluation of an XPath expression in the context of a specific node
;; for simplicity, it's not implemented here
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment