1

Example:

So how can I do this with clojure?

Advance so far

    (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))

But this will fail if the xml tags are not in proper order:

"<simple>  <code> content1 <x> r </x> </code>  </simple>"
vs
"<simple>  <code> <x> r </x> content 2 </code>  </simple>"
David Rz Ayala
  • 2,165
  • 1
  • 20
  • 22
  • What is the expected output when the tags and content are not in order? Do the two xmls always have the same number of children or can they differ? – juan.facorro Feb 26 '14 at 21:57
  • Check this question out http://stackoverflow.com/a/174071/1552130 with some java interop you may get an easier solution. – KobbyPemson Feb 27 '14 at 14:08
  • Yes, that is what I did at the end. Use JAVA interop. Thanks! – David Rz Ayala Feb 27 '14 at 20:48
  • You really shouldn't need Java - Clojure is a far easier language for xml manipulation. But it's quite unclear what you want the merge to do - should it be assuming a single 'x' child in a sea of content? What happens if there is no text before or after the 'x'? What if there is more than one 'x'? Are there other tags possible inside the 'code' block? – Korny Mar 15 '14 at 19:56

0 Answers0