unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: Andrea Girotto <andrea.girotto@gmail.com>
To: 15359@debbugs.gnu.org
Subject: bug#15359: Closing tag on empty elements for sxml->xml.
Date: Thu, 12 Sep 2013 10:48:04 +0200	[thread overview]
Message-ID: <CAEhjTVW+FV3QuwESFr-5JSUwiJD5Jdw2mun=rXF2aFR_8s6efA@mail.gmail.com> (raw)


[-- 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


                 reply	other threads:[~2013-09-12  8:48 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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='CAEhjTVW+FV3QuwESFr-5JSUwiJD5Jdw2mun=rXF2aFR_8s6efA@mail.gmail.com' \
    --to=andrea.girotto@gmail.com \
    --cc=15359@debbugs.gnu.org \
    /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).