Hi All,

I've been teaching myself clojure and in the process am working on a
small program to manipulate an XML file.  I figured this was a good
chance to try clojure.zip, but I'm having some difficulty.  Being a
newbie I strongly suspected myself - but when I posed the question on
IRC the conclusion was there appears to be a bug.

I boiled it down to the following:

(require ['clojure.xml :as 'xml]
         ['clojure.zip :as 'zip])

(defn strip [root tag]
  (loop [loc root]
    (if (zip/end? loc)
      (zip/root loc)
      (recur (zip/next (if (= (:tag (zip/node loc)) tag)
                         (zip/remove loc)
                         loc))))))

(def data "<?xml version=\"1.0\"?><root><foo/><bar/></root>")
(def xml-tree (xml/parse (java.io.ByteArrayInputStream. (.getBytes
data))))
(def zip-root (zip/xml-zip xml-tree))

(xml/emit (strip zip-root :foo))
(xml/emit (strip zip-root :bar))

The first call to strip :foo works as expected, but the following call
to strip :bar throws an NPE within zip/remove.  On further inspection
we came clojure/zip.clj:234:

(if (branch? loc)
  (recur (-> loc down rightmost))
  loc))

According to the docs, branch? can return true for a loc that does not
currently have any children, so the suspicion is that it is an
insufficient test before moving down as happens in the line below.  I
can fix this for my simple test cases by replacing the above with:

(if (and (branch? loc) (not (empty? (children loc))))
  (recur (-> loc down rightmost))
  loc))

Being new to both clojure and zip I admit there's still a good chance
that I have no idea what I'm talking about :).

Cheers,
Jason

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to