aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2020-07-12 23:26:11 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2020-07-12 23:26:11 +0200
commitce6c337da3183395f8f628a593bcc9f89711a34b (patch)
tree770d6d7bf9c3ab68b609c462f85d8137d55ccfe5
parentAdd initial (sxml namespace). (diff)
downloadcalp-ce6c337da3183395f8f628a593bcc9f89711a34b.tar.gz
calp-ce6c337da3183395f8f628a593bcc9f89711a34b.tar.xz
SXML Namespace mappings.
-rw-r--r--module/sxml/namespace.scm33
1 files changed, 27 insertions, 6 deletions
diff --git a/module/sxml/namespace.scm b/module/sxml/namespace.scm
index 4e364cad..5a274098 100644
--- a/module/sxml/namespace.scm
+++ b/module/sxml/namespace.scm
@@ -12,11 +12,32 @@
(define (string-last string)
(string-ref string (1- (string-length string))))
-;; sxml, (U symbol string #f) → sxml
-;; NOTE possibly allow namespace to be a map between namespaces
-(define-public (move-to-namespace sxml namespace)
- (define nssymb
+;; Change the namespace for all tags in a given SXML tree.
+;; Takes either a fix namespace everything is moved to, or
+;; an assoc list which maps input namespaces to output namespaces.
+;; A namespace is a symbol, string, or #f for no namespace.
+;; keys in the assoc list can't be strings.
+;; @example
+;; (move-to-namespace '(test) '((#f . NEW)))
+;; => (NEW:test)
+;; (move-to-namespace '(a:a (b:b)) '((a . b) (b . a)))
+;; => (b:a (a:b))
+;; (move-to-namespace '(a:a (b:b)) #f)
+;; => (a (b))
+;; (move-to-namespace '(a:a (b:b)) 'c)
+;; => (c:a (c:b))
+;; @end example
+;; sxml, (U symbol string #f (alist (U #f symbol) (U symbol string #f))) → sxml
+(define-public (move-to-namespace sxml namespace-map)
+
+ (define (nssymb key)
+ (define namespace
+ (if (list? namespace-map)
+ (or (assoc-ref namespace-map key)
+ (error "No mapping for namespace" key))
+ namespace-map))
+
(cond
[(not namespace) '#{}#]
[(symbol? namespace)
@@ -30,8 +51,8 @@
(define (ns tag)
(call-with-values (lambda () (symbol-split tag))
(case-lambda
- [(ns tag) (symbol-append nssymb tag)]
- [(tag) (symbol-append nssymb tag)])))
+ [(ns tag) (symbol-append (nssymb ns) tag)]
+ [(tag) (symbol-append (nssymb #f) tag)])))
(pre-post-order
sxml