Skip to content

Instantly share code, notes, and snippets.

@mprentice
Created July 6, 2011 17:53
Show Gist options
  • Save mprentice/1067874 to your computer and use it in GitHub Desktop.
Save mprentice/1067874 to your computer and use it in GitHub Desktop.
GraphML target update
(def mytestxml
"<graphml>
<graph edgedefault='directed'>
<node id='n1' />
<node id='n2' />
<node id='n3' />
<node id='n4' />
<edge id='e1' source='n1' target='n3' />
<edge id='e2' source='n2' target='n3' />
</graph>
</graphml>")
(def mytest (clojure.xml/parse (java.io.ByteArrayInputStream. (.getBytes mytestxml))))
(require '[clojure.zip :as zip]
'[clojure.contrib.zip-filter.xml :as zf])
(defn edit-target
"Edit target attribute of a GraphML edge"
([new-target]
(fn [node] (edit-target node new-target)))
([node new-target]
(assoc node
:attrs
(assoc (:attrs node) :target new-target))))
;;; Code below doesn't work: returns a list of xml struct-maps
;;; where one n3 target is changed to n4 target
;;; Also returns empty list if no n3 target found
(defn update-edges
"Update edge targets from old-target to new-target"
[root old-target new-target]
(zf/xml-> (zip/xml-zip root)
:graph
:edge
(zf/attr= :target old-target)
#(zip/edit % edit-target new-target)
zip/root))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment