[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: SXML example showing diff between node-reduce and node-join
From: |
Ricardo Wurmus |
Subject: |
Re: SXML example showing diff between node-reduce and node-join |
Date: |
Mon, 11 Jan 2021 23:01:25 +0100 |
User-agent: |
mu4e 1.4.13; emacs 27.1 |
Hi Matt,
> I'm going through the (sxml xpath) low-level routines to try to get a
> solid understanding of what they do. I just can't come up with an example
> to show how node-reduce and node-join differ. If someone could provide
> such an example I would really appreciate it. Takers?
Here’s an example:
--8<---------------cut here---------------start------------->8---
(define sxml
(xml->sxml "\
<foo>
<bar>baz</bar>
<wow>great</wow>
<boo>
<wow>
<b>superb</b>
</wow>
</boo>
<bye>
<hello>
<world>
<excellent>
<wow>today</wow>
</excellent>
</world>
</hello>
</bye>
<greet>
<hello>
<world>
<wow>yesterday</wow>
</world>
</hello>
</greet>
</foo>"
#:trim-whitespace? #true))
(define wow-converter
(lambda (node-or-nodeset)
(if (and (string? node-or-nodeset)
(string=? "yesterday" node-or-nodeset))
'(new "WOW!")
node-or-nodeset)))
(define joined
((node-join (sxpath '(//))
(node-trace "visiting")
wow-converter
(sxpath '(*text*)))
sxml))
(define reduced
((node-reduce (sxpath '(//))
(node-trace "visiting")
wow-converter
(sxpath '(*text*)))
sxml))
--8<---------------cut here---------------end--------------->8---
The node-trace converter already shows that they behave differently.
The wow-converter only gets to see the plain string “yesterday” when it
is used with node-join.
Clearly, this is not the best example one could come up with, but I hope
it’s a start.
--
Ricardo