我有一些XML,我正在使用clojure.data.zip.xml
进行争论。该XML具有与嵌套元素同名的元素:
<things>
<thing>
<thing>Want this</thing>
<thingattr>...but not this</thingattr>
</thing>
<thing>
<thing>Select this</thing>
<thingattr>...again, not this</thingattr>
</thing>
</things>
然而,使用clojure.data.zip.xml中的xml->
函数来访问内部元素似乎无法实现。但是,如果嵌套元素与父元素的名称不同,则所有元素都按预期工作:
(ns xmlzip.core
(:require [clojure.xml :as xml]
[clojure.zip]
[clojure.data.zip.xml :as z])
(:gen-class))
(defn parse [s]
(clojure.xml/parse
(java.io.ByteArrayInputStream. (.getBytes s))))
(def example1 (clojure.zip/xml-zip (parse "
<things>
<thing>
<thing>Want this</thing>
<thingattr>...but not this</thingattr>
</thing>
<thing>
<thing>Select this</thing>
<thingattr>...again, not this</thingattr>
</thing>
</things>
")))
(def example2 (clojure.zip/xml-zip (parse "
<things>
<thing>
<subthing>Want this</subthing>
<thingattr>...but not this</thingattr>
</thing>
<thing>
<subthing>Select this</subthing>
<thingattr>...again, not this</thingattr>
</thing>
</things>
")))
(z/xml-> example2 :thing :subthing z/text)
;; ("Want this" "Select this") ;hooray!
(z/xml-> example1 :thing :thing z/text)
;; ("Want this...but not this" "Select this...again, not this") ;boo, hiss.
我怀疑这可能是clojure.data.zip.xml
中未解决的问题,但我希望如果我只是误解了如何使用该库,有人可以告诉我正确的用法。