* sxml: avoiding intermixed text?
@ 2011-01-10 11:21 Aleix Conchillo Flaqué
2011-01-13 10:47 ` [SOLVED] " Aleix Conchillo Flaqué
0 siblings, 1 reply; 4+ messages in thread
From: Aleix Conchillo Flaqué @ 2011-01-10 11:21 UTC (permalink / raw)
To: guile-user
Hi!
is there an easy way to avoid intermixed text from xml? For example,
given this XML:
<?xml version="1.0"?>
<test>
<check><value>empty</value></check>
<check>
<value>hi!
this is just a test!
</value>
</check>
</test>
the following code:
-------------
(use-modules (sxml simple))
(use-modules (sxml xpath))
(let ((sxml (with-input-from-file "test.xml" xml->sxml)))
(map
(lambda (c) (pk (cdr c)))
((sxpath '(// check)) sxml)))
-------------
Returns:
-------------
;;; (((value "empty")))
;;; (("
" (value "hi!
this is just a test!
") "
"))
-------------
I'd like to get rid of the (car) of the second print. Or saying it in
another way, I want to get rid of all text that is not a leaf.
I'm using xpath to find my elements, which is what I found easiest from
all the sxml stuff. May be I should do it some other way.
Any help would be welcome!
Cheers,
Aleix
^ permalink raw reply [flat|nested] 4+ messages in thread
* [SOLVED] Re: sxml: avoiding intermixed text?
2011-01-10 11:21 sxml: avoiding intermixed text? Aleix Conchillo Flaqué
@ 2011-01-13 10:47 ` Aleix Conchillo Flaqué
2011-01-13 10:53 ` Thien-Thi Nguyen
0 siblings, 1 reply; 4+ messages in thread
From: Aleix Conchillo Flaqué @ 2011-01-13 10:47 UTC (permalink / raw)
To: guile-user
When I asked the question I thought I could use some sxml function to do
this job. Particularly, I found out a way to do it with the SXML
modifications explained here:
http://www.modis.ispras.ru/Lizorkin/sxml-tutorial.html#hevea:modif
Unfortunately this is not available in Guile. So, This function removes
whitespace text nodes from an sxml.
(define (remove-whitespace-nodes sxml)
(define (node-fix node)
(cond ((symbol? node) (list node))
((string? node) (if (string-null? (string-trim node))
#nil
(list node)))
(else (list (remove-whitespace-nodes node)))))
(let loop ((node sxml) (result '()))
(cond ((null? node) result)
(else (loop (cdr node)
(append result (node-fix (car node))))))))
I'm new to scheme, so a code review is really welcome!
Cheers,
Aleix
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [SOLVED] Re: sxml: avoiding intermixed text?
2011-01-13 10:47 ` [SOLVED] " Aleix Conchillo Flaqué
@ 2011-01-13 10:53 ` Thien-Thi Nguyen
2011-01-13 11:20 ` Aleix Conchillo Flaqué
0 siblings, 1 reply; 4+ messages in thread
From: Thien-Thi Nguyen @ 2011-01-13 10:53 UTC (permalink / raw)
To: guile-user
() Aleix Conchillo Flaqué <aconchillo@gmail.com>
() Thu, 13 Jan 2011 11:47:26 +0100
I'm new to scheme, so a code review is really welcome!
I would replace
(let loop ((node sxml) (result '()))
(cond ((null? node) result)
(else (loop (cdr node)
(append result (node-fix (car node)))))))
with
(map node-fix sxml)
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [SOLVED] Re: sxml: avoiding intermixed text?
2011-01-13 10:53 ` Thien-Thi Nguyen
@ 2011-01-13 11:20 ` Aleix Conchillo Flaqué
0 siblings, 0 replies; 4+ messages in thread
From: Aleix Conchillo Flaqué @ 2011-01-13 11:20 UTC (permalink / raw)
To: guile-user
On 01/13/2011 11:53 AM, Thien-Thi Nguyen wrote:
> I would replace
>
> (let loop ((node sxml) (result '()))
> (cond ((null? node) result)
> (else (loop (cdr node)
> (append result (node-fix (car node)))))))
>
> with
>
> (map node-fix sxml)
This would only change whitespaces for #nil... but...
(delete #nil (map node-fix sxml))
works solves this.
Thank you very much!
Aleix
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2011-01-13 11:20 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-01-10 11:21 sxml: avoiding intermixed text? Aleix Conchillo Flaqué
2011-01-13 10:47 ` [SOLVED] " Aleix Conchillo Flaqué
2011-01-13 10:53 ` Thien-Thi Nguyen
2011-01-13 11:20 ` Aleix Conchillo Flaqué
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).