From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ricardo Wurmus Newsgroups: gmane.lisp.guile.bugs Subject: bug#20339: sxml simple: sxml->xml mishandles namespaces? Date: Wed, 22 Apr 2015 16:29:32 +0200 Message-ID: <87fv7s1bjn.fsf@mango.localdomain> References: <20150415194714.GA30295@tuxteam.de> <87oamh25sc.fsf@mango.localdomain> <20150421094438.GA22715@tuxteam.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1429713089 14273 80.91.229.3 (22 Apr 2015 14:31:29 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 22 Apr 2015 14:31:29 +0000 (UTC) Cc: 20339@debbugs.gnu.org To: tomas@tuxteam.de Original-X-From: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Wed Apr 22 16:31:19 2015 Return-path: Envelope-to: guile-bugs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1YkvgM-0007uT-PD for guile-bugs@m.gmane.org; Wed, 22 Apr 2015 16:31:19 +0200 Original-Received: from localhost ([::1]:35533 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YkvgL-0004Ds-Mg for guile-bugs@m.gmane.org; Wed, 22 Apr 2015 10:31:17 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:57322) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YkvgD-0004Dk-7m for bug-guile@gnu.org; Wed, 22 Apr 2015 10:31:14 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Ykvg7-0005Yy-Ei for bug-guile@gnu.org; Wed, 22 Apr 2015 10:31:09 -0400 Original-Received: from debbugs.gnu.org ([140.186.70.43]:46850) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ykvg7-0005Yn-Ai for bug-guile@gnu.org; Wed, 22 Apr 2015 10:31:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.80) (envelope-from ) id 1Ykvg6-0001Fe-Hv for bug-guile@gnu.org; Wed, 22 Apr 2015 10:31:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Ricardo Wurmus Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Wed, 22 Apr 2015 14:31:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 20339 X-GNU-PR-Package: guile X-GNU-PR-Keywords: Original-Received: via spool by 20339-submit@debbugs.gnu.org id=B20339.14297130154707 (code B ref 20339); Wed, 22 Apr 2015 14:31:02 +0000 Original-Received: (at 20339) by debbugs.gnu.org; 22 Apr 2015 14:30:15 +0000 Original-Received: from localhost ([127.0.0.1]:36626 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1YkvfK-0001Dp-DS for submit@debbugs.gnu.org; Wed, 22 Apr 2015 10:30:15 -0400 Original-Received: from sender1.zohomail.com ([74.201.84.162]:52364) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1YkvfF-0001Dd-DZ for 20339@debbugs.gnu.org; Wed, 22 Apr 2015 10:30:11 -0400 Original-Received: from localhost (89.15.238.113 [89.15.238.113]) by mx.zohomail.com with SMTPS id 1429712981639612.5748451576998; Wed, 22 Apr 2015 07:29:41 -0700 (PDT) In-reply-to: <20150421094438.GA22715@tuxteam.de> X-Zoho-Virus-Status: 1 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 140.186.70.43 X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Original-Sender: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.bugs:7774 Archived-At: --=-=-= Content-Type: text/plain >> Since xml->sxml accepts a namespace alist I suppose it would make sense >> to extend sxml->xml to do the same. Attached is a minimal patch to extend "sxml->xml" such that it accepts an optional keyword argument "namespaces" with an alist of prefixes to URLs, analogous to "xml->sxml". When the namespaces alist is provided, "xmlns:prefix=url" attributes are prepended to the element's list of attributes. ;; Define SVG document with namespaces (define the-svg " ") ;; Define alist of namespaces (define ns '((svg . "http://www.w3.org/2000/svg") (xlink . "http://www.w3.org/1999/xlink"))) ;; Convert to SXML, abbreviate namespaces according to ns alist (define the-sxml (xml->sxml the-svg #:namespaces ns)) ;; Convert back to XML (sxml->xml the-sxml #:namespaces ns) => Does this do what you want? ~~ Ricardo --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Write-XML-namespaces-when-serializing.patch >From 81fa92ad0c5537c41419fa1e55c6130bf0558c9f Mon Sep 17 00:00:00 2001 From: rekado Date: Wed, 22 Apr 2015 13:09:27 +0200 Subject: [PATCH] Write XML namespaces when serializing. * module/sxml/simple.scm (sxml->xml): Add optional keyword argument "namespaces". --- 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 present." @@ -322,7 +323,7 @@ present." (let ((tag (car tree))) (case tag ((*TOP*) - (sxml->xml (cdr tree) port)) + (sxml->xml (cdr tree) port #:namespaces namespaces)) ((*ENTITY*) (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)) tree))) ((string? tree) (string->escaped-xml tree port)) ((null? tree) *unspecified*) -- 2.1.0 --=-=-=--