9

clojure.contrib の zip-filter.xml を介してアクセスされる xml ツリーを慣用的に変更する方法について、私は混乱しています。まったくこれをやろうとする必要がありますか、それとももっと良い方法がありますか?

次のようなダミーのxmlファイル「itemdb.xml」があるとします。

<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>

そして、私はいくつかのコードを持っています:

(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)))

この場合に clojure.zip 関数を適切に使用する方法と、それが zip-filter とどのように相互作用するかについては不明です。

この小さな例で特に奇妙な点を見つけた場合は、指摘してください。

4

2 に答える 2

8

まず、Fred の定義で を使用する必要があります:content(使用しないでください)。:contents

その変更により、次のように動作するようです。

(-> (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

あなたappend-item2は非常に似ています.2つの修正があります:

  1. zf/xml->一連のジッパー ロックを返します。zip/rightmost1つだけを受け入れるので、最初に1つを釣り上げる必要があります(したがって、first上記の )。

  2. zip/rootジッパーの変更が完了したら、 を使用して、基になるツリー (の変更されたバージョン) に戻る必要があります。

スタイルに関する最後の注意として、print+ format= printf. :-)

于 2010-05-20T17:40:59.677 に答える
5

create-item で :contents を :content と間違えたので、リテラルのリストよりもベクトルを優先する必要があります。

(私はより包括的な答えを出すつもりでしたが、Michalはすでにかなり良い答えを書いています。)

zip-filter の代替は 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))))
于 2010-05-20T18:00:38.527 に答える