I have the following XML structure:
(def xmlstr
\"
A AA
The Clojure standard APIs provide convenient functions for manipulating XML and other tree structures. Removing (leaf) nodes can be done on depth-first traversal using clojure.walk:
(require '[clojure.xml :as xml]
'[clojure.walk :as walk])
(def xmlstr
"
A AA
B BB
C CC
A AA
")
(def xmldoc (xml/parse (java.io.ByteArrayInputStream. (.getBytes xmlstr))))
(defn tag-matches [item tag]
(= (:tag item) tag))
(defn content-matches [item to-match]
((into #{} to-match)
(apply str (:content item))))
(defn match-criteria [item to-match]
(some #(and (tag-matches % :Type)
(content-matches % to-match))
(:content item)))
(defn mk-xml-walker [& to-remove]
(fn [form]
(if (and (vector? form)
(some #(tag-matches % :Item) form))
(filter (complement #(match-criteria % to-remove)) form)
form)))
(xml/emit (walk/postwalk (mk-xml-walker "B" "C") xmldoc))
For magical one-liners, you may also want to check out Specter which provides a very concise syntax for manipulating nested data structures, like XML.