unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#15359: Closing tag on empty elements for sxml->xml.
@ 2013-09-12  8:48 Andrea Girotto
  0 siblings, 0 replies; only message in thread
From: Andrea Girotto @ 2013-09-12  8:48 UTC (permalink / raw)
  To: 15359


[-- Attachment #1.1: Type: text/plain, Size: 448 bytes --]

Hello,
The sxml->xml procedure serializes the empty element always in the "short
form", i.e.:

<div/>

having to deal with transformations from xml to html, I needed to get:

<div></div>

so I modified sxml->xml (and element->xml) in simple.scm, to have:

scheme@(guile-user)> (sxml->xml '(*TOP* (tag)) #:closing-tag #t)


<tag></tag>

default is the old behaviour:

scheme@(guile-user)> (sxml->xml '(*TOP* (tag)))
<tag />

Regards,
Andrea Girotto

[-- Attachment #1.2: Type: text/html, Size: 924 bytes --]

[-- Attachment #2: 0001-Added-a-named-option-closing-tag-so-sxml-xml-outputs.patch --]
[-- Type: application/octet-stream, Size: 3031 bytes --]

From 7f08237b8b8eeb9fa01e7c7a776e2a8cc8e29c61 Mon Sep 17 00:00:00 2001
From: Andrea Girotto <andrea.girotto@gmail.com>
Date: Thu, 12 Sep 2013 10:13:56 +0200
Subject: [PATCH] Added a named option #:closing-tag so sxml->xml outputs
 <t></t> instead of <t/>

---
 module/sxml/simple.scm | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/module/sxml/simple.scm b/module/sxml/simple.scm
index 703ad91..8e08fcb 100644
--- a/module/sxml/simple.scm
+++ b/module/sxml/simple.scm
@@ -266,7 +266,7 @@ port."
   (attribute-value->xml value port)
   (display #\" port))
 
-(define (element->xml tag attrs body port)
+(define (element->xml tag attrs body closing-tag port)
   (check-name tag)
   (display #\< port)
   (display tag port)
@@ -287,7 +287,7 @@ port."
         (let lp ((body body))
           (cond
            ((pair? body)
-            (sxml->xml (car body) port)
+            (sxml->xml (car body) port #:closing-tag closing-tag)
             (lp (cdr body)))
            ((null? body)
             (display "</" port)
@@ -295,7 +295,13 @@ port."
             (display ">" port))
            (else
             (error "bad element body" tag body)))))
-      (display " />" port)))
+      (if closing-tag
+        (begin
+          (display #\> port)
+          (display "</" port)
+          (display tag port)
+          (display #\> port))
+        (display " />" port))))
 
 ;; FIXME: ensure name is valid
 (define (entity->xml name port)
@@ -311,10 +317,11 @@ 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 (closing-tag #f))
   "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."
+  ;; CLOSING-TAG: if #t empty tags are serialized: <tag></tag> otherwise (default case) <tag/>
   (cond
    ((pair? tree)
     (if (symbol? (car tree))
@@ -322,7 +329,7 @@ present."
         (let ((tag (car tree)))
           (case tag
             ((*TOP*)
-             (sxml->xml (cdr tree) port))
+             (sxml->xml (cdr tree) port #:closing-tag closing-tag))
             ((*ENTITY*)
              (if (and (list? (cdr tree)) (= (length (cdr tree)) 1))
                  (entity->xml (cadr tree) port)
@@ -336,9 +343,9 @@ present."
                     (attrs (and (pair? elems) (pair? (car elems))
                                 (eq? '@ (caar elems))
                                 (cdar elems))))
-               (element->xml tag attrs (if attrs (cdr elems) elems) port)))))
+               (element->xml tag attrs (if attrs (cdr elems) elems) closing-tag port)))))
         ;; A nodelist.
-        (for-each (lambda (x) (sxml->xml x port)) tree)))
+        (for-each (lambda (x) (sxml->xml x port #:closing-tag closing-tag)) tree)))
    ((string? tree)
     (string->escaped-xml tree port))
    ((null? tree) *unspecified*)
-- 
1.8.4


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2013-09-12  8:48 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-12  8:48 bug#15359: Closing tag on empty elements for sxml->xml Andrea Girotto

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).