[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sxml simple, sxml->xml and namespaces

From: Ricardo Wurmus
Subject: Re: sxml simple, sxml->xml and namespaces
Date: Mon, 20 Jun 2016 12:52:47 +0200
User-agent: mu4e 0.9.16; emacs 24.5.1

Andy Wingo <address@hidden> writes:

> Apologies for the long delay here.  I'm with you regarding namespaces
> and sxml->xml.  In the past I made sure to always get the namespaces
> attached to the root element via the @ xmlns attributes, and then have
> namespaced uses just be local names, not qnames, and that way sxml->xml
> works fine.  But, perhaps that doesn't cover all cases in a nice way.
> Do you still have thoughts on this patch?  Is the right thing for you?
> In any case we need better documentation in the manual about how to deal
> with namespaces and SXML, in practice, with examples.

Here is another proposal, mirroring what is done in “xml->sxml”:

>From 845badc7d4b748bc13c532e940b8a18ffb10f426 Mon Sep 17 00:00:00 2001
From: Ricardo Wurmus <address@hidden>
Date: Sun, 30 Aug 2015 10:57:00 +0200
Subject: [PATCH] sxml: Write XML namespaces when serializing.

* module/sxml/simple.scm (sxml->xml): Add optional keyword argument
 module/sxml/simple.scm | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/module/sxml/simple.scm b/module/sxml/simple.scm
index 703ad91..8cc20dd 100644
--- a/module/sxml/simple.scm
+++ b/module/sxml/simple.scm
@@ -311,7 +311,8 @@ port."
   (display str port)
   (display "?>" port))
-(define* (sxml->xml tree #:optional (port (current-output-port)))
+(define* (sxml->xml tree #:optional (port (current-output-port)) #:key
+                    (namespaces '()))
   "Serialize the sxml tree @var{tree} as XML. The output will be written
 to the current output port, unless the optional argument @var{port} is
@@ -322,7 +323,7 @@ present."
         (let ((tag (car tree)))
           (case tag
-             (sxml->xml (cdr tree) port))
+             (sxml->xml (cdr tree) port #:namespaces namespaces))
              (if (and (list? (cdr tree)) (= (length (cdr tree)) 1))
                  (entity->xml (cadr tree) port)
@@ -335,10 +336,16 @@ present."
              (let* ((elems (cdr tree))
                     (attrs (and (pair? elems) (pair? (car elems))
                                 (eq? '@ (caar elems))
-                                (cdar elems))))
-               (element->xml tag attrs (if attrs (cdr elems) elems) port)))))
+                                (cdar elems)))
+                    (xmlns (map (lambda (x)
+                                  (cons (symbol-append 'xmlns: (car x))
+                                        (cdr x)))
+                                namespaces)))
+               (element->xml tag
+                             (if attrs (append xmlns attrs) xmlns)
+                             (if attrs (cdr elems) elems) port)))))
         ;; A nodelist.
-        (for-each (lambda (x) (sxml->xml x port)) tree)))
+        (for-each (lambda (x) (sxml->xml x port #:namespaces namespaces)) 
    ((string? tree)
     (string->escaped-xml tree port))
    ((null? tree) *unspecified*)

~~ Ricardo

reply via email to

[Prev in Thread] Current Thread [Next in Thread]