Using zip-filter to remove nodes

40 views
Skip to first unread message

James Sofra

unread,
Feb 24, 2010, 8:09:27 PM2/24/10
to Clojure
Hi all,

I am trying to use (abuse? *) zip-filter to remove some nodes and
return the xml tree to me.
If I do something like this:
(xml1-> zipped-xml zf/descendants :model zip/remove zip/root)

I can remove one :model node, but what if I want to remove more than
one or all of the :model nodes?

If I do this:
(xml-> zipped-xml zf/descendants :model zip/remove zip/root)

I get multiple xml-trees each with one node removed from each. So I
wrote a function "zip-top" to take me back to the top (like zip/root
but returns the loc not node) and can do this:
(xml-> zipped-xml zf/descendants :model zip/remove zip-top zf/
descendants :model zip/remove zip/root)

and that will remove two of them but that is not really ideal I would
like to remove all of them ... any ideas? am I way off track? is there
better ways to manipulate xml trees in clojure? Enlive maybe? I
haven't played with it yet.

* I realise this probably abusing zip-filter since it is only supposed
to take predicates and zip/remove is not really a predicate.

James

Chouser

unread,
Feb 24, 2010, 10:37:01 PM2/24/10
to clo...@googlegroups.com

You have correctly identified the behavior of zip-filter and its
negative consequences. I didn't realize until too late that my
design for zip-filter essentially prevents using it to edit more
than a single filter result. I haven't thought deeply about what
could be done to fix it -- I think it would have to be pretty
radical. Instead of returning a lazy seq it would have to have
some kind of mechanism for continuation, or perhaps using a monad
or one of these new cells.

Christophe Grand's "enlive" lib address a lot of these problems
for html and some xml. I'd recommend you take a look at it and
see if it would work in your case.

--Chouser
http://joyofclojure.com/

Reply all
Reply to author
Forward
0 new messages