From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Josef Wolf Newsgroups: gmane.lisp.guile.user Subject: Converting s-expressions to XML Date: Thu, 17 Jun 2010 17:00:53 +0200 Message-ID: <20100617150052.GA4076@raven.wolf.lan> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1276787460 2189 80.91.229.12 (17 Jun 2010 15:11:00 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Thu, 17 Jun 2010 15:11:00 +0000 (UTC) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Thu Jun 17 17:10:55 2010 connect(): No such file or directory Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1OPGk0-000137-Dw for guile-user@m.gmane.org; Thu, 17 Jun 2010 17:10:52 +0200 Original-Received: from localhost ([127.0.0.1]:32807 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OPGjz-00042R-KR for guile-user@m.gmane.org; Thu, 17 Jun 2010 11:10:51 -0400 Original-Received: from [140.186.70.92] (port=55741 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OPGjP-0003fW-KJ for guile-user@gnu.org; Thu, 17 Jun 2010 11:10:18 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OPGjO-0006PZ-8N for guile-user@gnu.org; Thu, 17 Jun 2010 11:10:15 -0400 Original-Received: from quechua.inka.de ([193.197.184.2]:52184 helo=mail.inka.de) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OPGjN-0006P5-W9 for guile-user@gnu.org; Thu, 17 Jun 2010 11:10:14 -0400 Original-Received: from raven.inka.de (uucp@[127.0.0.1]) by mail.inka.de with uucp (rmailwrap 0.5) id 1OPGjL-0002m4-7y; Thu, 17 Jun 2010 17:10:11 +0200 Original-Received: by raven.inka.de (Postfix, from userid 1000) id 304397614F; Thu, 17 Jun 2010 17:00:53 +0200 (CEST) Mail-Followup-To: Josef Wolf , guile-user@gnu.org Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4-2.6 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:7881 Archived-At: Hello, I am trying to write a (simple) function to convert s-expressions to XML. I've come up with following function, which (somehow) works: (use-modules (ice-9 rdelim)) (use-modules (ice-9 pretty-print)) (define atom? (lambda (x) (and (not (pair? x)) (not (null? x))))) (define (indent string count) (if (< count 1) "" (string-append string (indent string (- count 1))))) (define (walklist expr level) (if (null? expr) '() (begin (sexp->xml (car expr) (+ level 1)) (walklist (cdr expr) level)))) (define (unknown->string expr) (let ((s (open-output-string))) (display expr s) (get-output-string s))) (define (sexp->xml expr . params) (let ((level (if (null? params) 0 (car params)))) (cond ((atom? expr) (simple-format #t "~A~A\n" (indent " " level) expr)) ((list? (car expr)) (sexp->xml (car expr) (+ level 1)) (walklist (cdr expr) level)) ; ((pair? expr) ; (simple-format #t "~A~A\n" (indent " " level) (car expr)) ; (simple-format #t "~A~A\n" (indent " " level) (cdr expr))) (#t (let ((s (unknown->string (car expr)))) (simple-format #t "~A~A~A~A\n" (indent " " level) "<" s ">") (walklist (cdr expr) level) (simple-format #t "~A~A~A~A\n" (indent " " level) "")))))) (sexp->xml '(person (name "myself") (address (street "somestreet" 2) (town 1234 "thistown") (country "wonderland") (test 'a b) ; (test1 . asd) (test2 '(asd "asd"))))) While this seems to work, it has some drawbacks, which I can not figure out how to get rid of them: - It doesn't work on pairs. When I uncomment the case which checks for pairs, the result is no longer XML. Instead, it looks more like the output of (display) - There is no way to tell symbols from strings in the XML output. - I'd like atoms to be enclosed into its tags without any whitespace. While it is easy to get rid of the indentation and the trailing newline, I can't figure out how to get rid of the newline that comes behind the opening tag Any hints? BTW: In addition, I'd appreciate any hints how to make this thing more scheme'ish. I think there is lots of potential for improvement here.