From: Ricardo Wurmus <rekado@elephly.net>
To: tomas@tuxteam.de
Cc: 20339@debbugs.gnu.org
Subject: bug#20339: sxml simple: sxml->xml mishandles namespaces?
Date: Wed, 22 Apr 2015 16:29:32 +0200 [thread overview]
Message-ID: <87fv7s1bjn.fsf@mango.localdomain> (raw)
In-Reply-To: <20150421094438.GA22715@tuxteam.de>
[-- Attachment #1: Type: text/plain, Size: 1874 bytes --]
>> 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 "<svg xmlns='http://www.w3.org/2000/svg'
xmlns:xlink='http://www.w3.org/1999/xlink'>
<rect x='5' y='5' width='20' height='20'
stroke-width='2' stroke='purple' fill='yellow'
id='rect1' />
<rect x='30' y='5' width='20' height='20'
ry='5' rx='8' stroke-width='2' stroke='purple' fill='blue'
xlink:href='#rect1' />
</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)
=> <svg:svg xmlns:svg="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<svg:rect y="5" x="5"
width="20"
stroke-width="2"
stroke="purple"
id="rect1"
height="20"
fill="yellow" />
<svg:rect xlink:href="#rect1"
y="5" x="30"
width="20"
stroke-width="2"
stroke="purple"
ry="5" rx="8"
height="20"
fill="blue" />
</svg:svg>
Does this do what you want?
~~ Ricardo
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Write-XML-namespaces-when-serializing.patch --]
[-- Type: text/x-patch, Size: 2329 bytes --]
From 81fa92ad0c5537c41419fa1e55c6130bf0558c9f Mon Sep 17 00:00:00 2001
From: rekado <rekado@elephly.net>
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
next prev parent reply other threads:[~2015-04-22 14:29 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-04-15 19:47 bug#20339: sxml simple: sxml->xml mishandles namespaces? tomas
2015-04-20 7:45 ` bug#20339: [PATCH] sxml->xml and namespaces: updated patch tomas
2015-04-21 9:24 ` bug#20339: sxml simple: sxml->xml mishandles namespaces? Ricardo Wurmus
2015-04-21 9:44 ` tomas
2015-04-22 14:29 ` Ricardo Wurmus [this message]
2015-04-23 6:57 ` tomas
2015-04-23 7:04 ` Ricardo Wurmus
2015-04-23 7:40 ` tomas
2015-04-25 20:25 ` tomas
2015-04-26 10:28 ` tomas
2016-06-23 19:32 ` Andy Wingo
2016-07-13 13:24 ` tomas
2016-07-13 18:08 ` tomas
2016-07-14 10:10 ` Andy Wingo
2016-07-14 10:26 ` tomas
2019-02-04 20:44 ` Ricardo Wurmus
2019-02-04 22:55 ` John Cowan
2019-02-05 9:12 ` Ricardo Wurmus
2019-02-05 12:57 ` Ricardo Wurmus
2019-04-08 12:14 ` tomas
2019-02-12 9:56 ` tomas
2019-02-12 20:30 ` Ricardo Wurmus
2019-05-03 10:46 ` bug#20339: Taking a step back (was: sxml simple: sxml->xml mishandles namespaces?) tomas
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87fv7s1bjn.fsf@mango.localdomain \
--to=rekado@elephly.net \
--cc=20339@debbugs.gnu.org \
--cc=tomas@tuxteam.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).