Skip to content

Instantly share code, notes, and snippets.

@mangosmoothie
Last active April 2, 2019 19:45
Show Gist options
  • Save mangosmoothie/a2b25025f5cf3484f30de40bd7d05042 to your computer and use it in GitHub Desktop.
Save mangosmoothie/a2b25025f5cf3484f30de40bd7d05042 to your computer and use it in GitHub Desktop.
remove keys with nil value from clojure.xml parsed xml file
;; remove keys with nil value from a data structure created by parsing an xml file with clojure.xml
;; to do this you first must convert the clojure.xml elements to regular maps since they are created
;; as structs and thus will not allow you to remove keys
(require '[com.rpl.specter :as r] '[clojure.xml :as xml])
(def ALL-ELEMENTS
(r/recursive-path
[] p
(r/if-path :content
(r/continue-then-stay [:content r/ALL p])
r/STAY)))
(def NIL-VALS
(r/recursive-path
[] p
(r/if-path map?
[r/MAP-VALS p]
(r/if-path vector?
[r/ALL p]
(r/if-path nil? r/STAY)))))
(def note-xsd
(->> (xml/parse "note.xsd")
(r/transform [ALL-ELEMENTS] #(into {} %))
(r/setval [NIL-VALS] r/NONE)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment