Search code examples
xmlclojure

Clojure: create a list of values extracted from record


I have a data structure of type "clojure.data.xml.Element" that looks like this (pretty printed):

{:tag :eSearchResult,
 :attrs {},
 :content
 ({:tag :Count, :attrs {}, :content ("16")}
  {:tag :RetMax, :attrs {}, :content ("16")}
  {:tag :RetStart, :attrs {}, :content ("0")}
  {:tag :IdList,
   :attrs {},
   :content
   ({:tag :Id, :attrs {}, :content ("28911150")}
    {:tag :Id, :attrs {}, :content ("28899394")}
    {:tag :Id, :attrs {}, :content ("28597238")}
    {:tag :Id, :attrs {}, :content ("28263281")}
    {:tag :Id, :attrs {}, :content ("28125459")}
    {:tag :Id, :attrs {}, :content ("26911135")}
    {:tag :Id, :attrs {}, :content ("26699345")}
    {:tag :Id, :attrs {}, :content ("26297102")}
    {:tag :Id, :attrs {}, :content ("26004019")}
    {:tag :Id, :attrs {}, :content ("25995331")}
    {:tag :Id, :attrs {}, :content ("25429093")}
    {:tag :Id, :attrs {}, :content ("25355095")}
    {:tag :Id, :attrs {}, :content ("25224593")}
    {:tag :Id, :attrs {}, :content ("24816246")}
    {:tag :Id, :attrs {}, :content ("24779721")}
    {:tag :Id, :attrs {}, :content ("24740865")})}

How do I extract the Ids from these records? In other words create a function that takes this data structure and returns a list of Id strings ("28911150" "28899394" ...)?

Thanks.


Solution

  • You can achieve that using the basic data accessors from Clojure:

    (->> data                            ; the input
         :content                        ; the content list
         (filter #(= :IdList (:tag %)))  ; only the IdLists
         (mapcat :content)               ; their content as one list
         (filter #(= :Id (:tag %)))      ; only the Ids
         (mapcat :content))              ; one long list of the strings therein