让我们假设有一些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样本看,我已经找到了如何获取对我感兴趣的单值。

我不知道我怎么会做类似返回对(C E)的文本值的列表。

编辑:

下面是一些工作的代码,但它是相当难看。不要求琐碎的重构,但有一个更好的方式,拉链让我们做到这一点?

(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)))
有帮助吗?

解决方案

根据您使用Clojure的版本,你可能会发现juxt功能非常有用。您发布的代码(仅相关部分):

(defn extract-data
  [xml] 
  (let [...]
    (map (juxt getASIN (comp getTitle getAttributes) (comp getAuthor getAttributes)) items))))

其他提示

我敢肯定有一个更好的方式,但这样做的工作:

(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"]))
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top