I'm looking for some useful idioms for transforming XML in Clojure.
I'm using the representation provided by clojure.xml and manipulating
it using clojure.zip, clojure.contrib.zip-filter and
clojure.contrib.zip-filter.xml.
Here's an XML showing the parts I'll be talking about.
<project>
<artifactId>hello</artifactId>
<groupId>group</group>
<version>1</version>
<parent>
<artifactId>parent</artifactId>
<groupId>group</group>
<version>1</version>
</parent>
</project>
1. I'd like to be able to remove project/groupId and project/version,
if it is present, but not error out if it's not present. Currently I'm
doing this:
(defn edit1-> [pom & path]
(let [loc (zip/xml-zip pom)]
(if-let [loc (apply zx/xml1-> (zip/xml-zip pom) path)]
(zip/root loc)
pom)))
(-> pom
(edit1-> :groupId zip/remove)
(edit1-> :version zip/remove)))
2. I'd like to be able to alter the values in
project/parent/artifacId, project/parent/groupId and
project/parent/version.
For this *very specific* case where I know that project/parent/groupId
exists and I know that it is textual and I know that it contains
exactly one string, I can do this:
(-> pom
;...
(edit1-> :parent :groupId zip/down #(zip/replace % "GROUP"))))
What if project/parent/groupId doesn't exist? What if project/parent
doesn't exist? I don't suppose there's something like assoc-in? in the
zip world?
All the half-considered schemes I've come up with seem brittle or
needlessly obtuse or make too many assumptions. Part of that may be
that I'm still stuck in the XSLT mindset and Clojure isn't XSLT.
suggestions for a learner?
// ben
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en