如何在Clojure中合并两个XML?

时间:2014-02-26 20:03:04

标签: xml clojure zip

示例:

那么我怎么能用clojure做到这一点呢?

到目前为止

    (defn zip-str [s]
      (clojure.zip/xml-zip (clojure.xml/parse (java.io.ByteArrayInputStream. (.getBytes s)))))


    (def x (zip-str "<simple>  <code> content1 <x> r </x> </code>  </simple>"))
    (def y (zip-str "<simple>  <code> content2 <x> s </x> </code>  </simple>"))


    (defn xml-merge [a b]
      (if (not (string? a))
          (merge-with 
            (fn [x y]
              (cond 
                (and (map? x) (= (:tag x) (:tag y)))
                    (do (xml-merge x y) )
                (vector? x)
                  (map #(xml-merge %1 %2) x y)
                :else 
                  y))
            a b)
           [a b]))


    (xml-merge (get y 0) (get x 0))

但如果xml标签的顺序不正确,则会失败:

"<simple>  <code> content1 <x> r </x> </code>  </simple>"
vs
"<simple>  <code> <x> r </x> content 2 </code>  </simple>"

0 个答案:

没有答案