我有一些我正在使用的 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>
xml->
但是,使用clojure.data.zip.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
,但如果我只是误解了如何使用该库,我希望有人能告诉我正确的用法。