4

次のようなXMLがあるとします。

<a>
  <b>
    <c>text</c>
    <d>
      <e>text</e>
      <f>
        ... lots of cruft here ..
      </f>
    </d>
  </b>
  <b>
    ...
  </b>
  <!-- more b sub-trees --> 
</a>

ここで、zip_filter / xml.cljのサンプルを調べて、関心のある単一の値を取得する方法を見つけました。

(ce)のテキスト値のペアのリストを返すようなことをどのように行うのか疑問に思っています。

編集:

ここにいくつかの動作するコードがありますが、それはかなり醜いです。些細なリファクタリングを求めていませんが、ジッパーがこれを行うためのより良い方法はありますか?

(defn extract-data [xml] 
  (let [items (x/xml-> xml zf/descendants :Item)     ;items not top-level
        getAttributes  #(x/xml1-> % :ItemAttributes) ;items have itemattributes
        getASIN        #(x/xml1-> % :ASIN x/text)    ;items have ASINs
        getTitle       #(x/xml1-> % :Title x/text)   ;itemattributes have Titles
        getAuthor      #(x/xml1-> % :Author x/text)] ;itemattributes have Authors
    (map 
       ;build a function to get everything we need from the items, and apply
      #(let [attributes (getAttributes %)] ;get the attributes, we'll use it twice
         (list 
           (getASIN %) 
           (getTitle attributes) 
           (getAuthor attributes)))
      items)))
4

2 に答える 2

4

使用する clojure のバージョンによっては、このjuxt関数が役立つ場合があります。投稿されたコード (関連する部分のみ):

(defn extract-data
  [xml] 
  (let [...]
    (map (juxt getASIN (comp getTitle getAttributes) (comp getAuthor getAttributes)) items))))
于 2010-01-11T12:34:19.073 に答える
2

もっと良い方法があると確信していますが、これは仕事をします:

(letfn [(get-tag [tag coll] (:content (first (filter #(= tag (:tag %)) coll))))]
  (map #(list (get-tag :c %) (get-tag :e (get-tag :d %)))
       (map :content (:content (clojure.xml/parse "foo.xml")))))

結果は

((["ctext1"] ["etext1"]) (["ctext2"] ["etext2"]))
于 2010-01-11T04:26:31.770 に答える