views:

291

answers:

2

I'm confused as how to idiomatically change a xml tree accessed through clojure.contrib's zip-filter.xml. Should be trying to do this at all, or is there a better way?

Say that I have some dummy xml file "itemdb.xml" like this:

<itemlist> 
  <item id="1">
    <name>John</name>
    <desc>Works near here.</desc>
  </item>
  <item id="2">
    <name>Sally</name>
    <desc>Owner of pet store.</desc>
  </item>
</itemlist>

And I have some code:

(require '[clojure.zip :as zip]
  '[clojure.contrib.duck-streams :as ds]
  '[clojure.contrib.lazy-xml :as lxml]
  '[clojure.contrib.zip-filter.xml :as zf]) 

(def db (ref (zip/xml-zip (lxml/parse-trim (java.io.File. "itemdb.xml")))))

;; Test that we can traverse and parse.
(doall (map #(print (format "%10s: %s\n"
       (apply str (zf/xml-> % :name zf/text))
       (apply str (zf/xml-> % :desc zf/text))))
     (zf/xml-> @db :item)))

;; I assume something like this is needed to make the xml tags
(defn create-item [name desc]
  {:tag :item
   :attrs {:id "3"}
   :contents
   (list {:tag :name :attrs {} :contents (list name)}
         {:tag :desc :attrs {} :contents (list desc)})})

(def fred-item (create-item "Fred" "Green-haired astrophysicist."))

;; This disturbs the structure somehow
(defn append-item [xmldb item]
  (zip/insert-right (-> xmldb zip/down zip/rightmost) item))

;; I want to do something more like this
(defn append-item2 [xmldb item]
  (zip/insert-right (zip/rightmost (zf/xml-> xmldb :item)) item))

(dosync (alter db append-item2 fred-item))

;; Save this simple xml file with some added stuff.
(ds/spit "appended-itemdb.xml"
    (with-out-str (lxml/emit (zip/root @db) :pad true)))

I am unclear about how to use the clojure.zip functions appropriately in this case, and how that interacts with zip-filter.

If you spot anything particularly weird in this small example, please point it out.

+4  A: 

Firstly, you should use :content (and not :contents) in your definition of Fred.

With that change in place, the following seems to work:

(-> (zf/xml-> @db :item) ; a convenient way to get to the :item zipper locs
    first                ; but we actually need just one
    zip/rightmost        ; let's move to the rightmost sibling of the first :item
                         ; (which is the last :item in this case)
    (zip/insert-right fred-item) ; insert Fred to the right
    zip/root)            ; get the modified XML map,
                         ; which is the root of the modified zipper

Your append-item2 is very similar, there are just two corrections to make:

  1. zf/xml-> returns a sequence of zipper locs; zip/rightmost accepts just one, so you have to fish one out first (hence the first in the above);

  2. after you're done modifying the zipper, you need to use zip/root to get back at (the modified version of) the underlying tree.

As a final note on style, print + format = printf. :-)

Michał Marczyk
Thanks for the corrections! There is still some strange behavior here that seems to be solved by adding zip/xml-zip after the final zip/root in your code. Failure to add that extra line prevents you from doing more than one append...apparently zip/root doesn't return a zipper, just another location, so the type returned by the append-item will still not be quite right, but good enough for the lxml/emit at the end...Had to laugh about the printf note... amazing how many times you can stare at something without seeing the obvious. ;-)
ivar
+2  A: 

In create-item you mistyped :contents for :content and you should prefer vectors to lists for literals.

(I was going to make a more comprehensive answer but Michal as already written a pretty good one.)

An alternative to zip-filter is Enlive:

(require '[net.cgrand.enlive-html :as e]) ;' <- fix SO colorizer

(def db (ref (-> "itemdb.xml" java.io.File. e/xml-resource))

(defn create-item [name desc]
  {:tag :item
   :attrs {:id "3"}
   :content [{:tag :name :attrs {} :content [name]}
             {:tag :desc :attrs {} :content [desc]}]})

(def fred-item (create-item "Fred" "Green-haired astrophysicist."))

(dosync (alter db (e/transformation [:itemlist] (e/append fred-item))))
cgrand
I will have to give enlive a closer look after looking at [http://github.com/swannodette/enlive-tutorial/]...
ivar