使用clojure xml zipper返回多个值 [英] returning multiple values using clojure xml zipper
问题描述
假设我们有一些像这样的XML:
< a>
< b>
< c> text< / c>
< d>
< e> text< / e>
< f>
...很多cruft here ..
< / f>
< / d>
< / b>
< b>
...
< / b>
<! - more b sub-trees - >
< / a>
现在,通过zip_filter / xml.clj中的示例,我想出了如何获取我感兴趣的单个值。
我想知道如何做一些事情,如返回一个文本值对(ce)的对的列表。 p>
编辑:
这里有一些工作代码,但是很丑陋。不要求轻微的重构,但是有一个更好的方式拉链给我们这样做吗?
(defn extract-data [xml]
(let [items(x / xml-> xml zf /子项:项目);项目不是顶级
getAttributes#(x / xml1->%:ItemAttributes);项目有itemattributes
getASIN#(x / xml1->%:ASIN x / text ); items具有ASIN
getTitle#(x / xml1->%:Title x / text); itemattributes has Titles
getAuthor#(x / xml1->%:Author x / text) ; itemattributes have Authors
(map
;构建一个函数来从项目中获取我们需要的一切,并应用
#(let [attributes(getAttributes%)];获取属性,将使用它两次
(list
(getASIN%)
(getTitle属性)
(getAuthor属性)))
items)))
根据你使用的clojure版本,你可能会发现 juxt
函数有用。您发布的代码(仅限相关部分):
(defn extract-data
[xml]
let [...]
(map(juxt getASIN(comp getTitle getAttributes))item))))
Lets suppose we have some XML like so:
<a>
<b>
<c>text</c>
<d>
<e>text</e>
<f>
... lots of cruft here ..
</f>
</d>
</b>
<b>
...
</b>
<!-- more b sub-trees -->
</a>
Now, looking through the samples in zip_filter/xml.clj, I've figured out how to get to single values that I'm interested in.
I'm wondering how I would do something like return a list of pairs of text values of (c e).
EDIT:
Here is some working code, but it's pretty ugly. Not asking for trivial refactoring, but is there a nicer way that zippers give us to do this?
(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)))
Depending on the clojure version you use, you might find the juxt
function useful. Your posted code (only relevant parts):
(defn extract-data
[xml]
(let [...]
(map (juxt getASIN (comp getTitle getAttributes) (comp getAuthor getAttributes)) items))))
这篇关于使用clojure xml zipper返回多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!