使用clojure xml zipper返回多个值
发布时间:2020-12-16 23:04:05 所属栏目:百科 来源:网络整理
导读:让我们假设我们有一些像这样的 XML: a b ctext/c d etext/e f ... lots of cruft here .. /f /d /b b ... /b !-- more b sub-trees -- /a 现在,通过zip_filter / xml.clj中的示例,我已经找到了如何获得我感兴趣的单个值. 我想知道如何返回(c e)的文本值对列
让我们假设我们有一些像这样的
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)))) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |