Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Clojure中的XML拉链构建自定义结构的惯用方法_Xml_Clojure - Fatal编程技术网

从Clojure中的XML拉链构建自定义结构的惯用方法

从Clojure中的XML拉链构建自定义结构的惯用方法,xml,clojure,Xml,Clojure,比如说,我正在解析一个RSS提要,并希望从中提取一部分信息 (def feed (-> "http://..." clojure.zip/xml-zip clojure.xml/parse)) 我可以分别获得链接和标题: (xml-> feed :channel :item :link text) (xml-> feed :channel :item :title text) 然而,如果不多次穿过拉链,我就无法同时取出它们 (let [feed (-> "http:/

比如说,我正在解析一个RSS提要,并希望从中提取一部分信息

(def feed (-> "http://..." clojure.zip/xml-zip clojure.xml/parse))
我可以分别获得链接和标题:

(xml-> feed :channel :item :link text)
(xml-> feed :channel :item :title text)
然而,如果不多次穿过拉链,我就无法同时取出它们

(let [feed (-> "http://..." clojure.zip/xml-zip clojure.xml/parse)]
    (zipmap 
        (xml-> feed :channel :item :link text)
        (xml-> feed :channel :item :title text)))
…或其变体,涉及将多个序列映射到一个函数,该函数以增量方式构建映射,例如,
assoc

我不仅要多次遍历序列,而且序列也有单独的状态,所以可以说元素必须“对齐”。也就是说,在比RSS更复杂的情况下,特定元素中可能缺少一个子元素,从而使其中一个序列短一个(没有间隙)。所以结果可能是不正确的

有更好的方法吗?或者,事实上,这是你在Clojure中的做法吗?

这是怎么回事

(reduce (fn [h item] 
          (assoc h (xml1-> item :title text) 
                   (xml1-> item :link text))) 
        {} (xml-> feed :channel :item))