Fork me on GitHub
#clojure-uk
<
2018-02-09
>
paulspencerwilliams07:02:05

So I've enjoyed my foray into zippers this week. I wanted to make a small change to a single value in ~ 100 xml files and reading / parsing / zipping xml

(clojure.zip/xml-zip (clojure.data.xml/parse ( "path-to-data.xml")))
worked a treat. Using zip/down, and zip/edit was also pretty easy to grok. Now I just needed to write back the file. Simple, just invoke clojure.data.xml/emit on the zip? Yes, on simple xml. However my gpx contains xml and emit fails with
XMLStreamException Prefix cannot be null  com.sun.xml.internal.stream.writers.XMLStreamWriterImpl.writeAttribute (XMLStreamWriterImpl.java:575)
probably related to https://groups.google.com/forum/#!topic/clojure/Um3aC_z0--4 Am I being stupid? Should I fall back to clojure.xml? How would you approach this seemingly simple task? I ask as most googling of zippers + xml return pages circa 2014 so am I just doing this an old fashioned error-prone or non-idiomatic way?

dominicm07:02:34

Not out of date at all, it's just that they solve a specific problem that few people have. After the hype was over, people had little left to say.

dominicm07:02:18

We use clojure data xml (the recent alphas), are you using that too?

paulspencerwilliams07:02:43

Oh no, I'm using the stable org.clojure/data.xml "0.0.8" release. I'll try the alpha at lunchtime. Cheers.

paulspencerwilliams12:02:48

That worked a treat, cheers. Now, I've just got to work out the round trip qname retrieval / setting as I want to do a similar string change in the xml, nothing more complex.

Sam H13:02:52

@U064B4L0K any recommendations on materials you found useful to understand zippers? Not looked into them at all

paulspencerwilliams13:02:18

So, without attempting to apply namespaces, emit-str understandable generates a

ExceptionInfo Auto-generating prefixes is not supported for content-qnames. Please declare all URIs used in content qnames.  clojure.core/ex-info (core.clj:4617)
, and adding all the namespaces for
gpx xmlns:gh="" xmlns:gs="" xmlns:os="" xmlns:xsi="" xmlns="" creator="OSMaps" version="1.1" xsi:schemaLocation="">
like
(xml/emit-str (first (edit-title (second all-files))) {:xmlns/gh "" :xmlns/gs "" :xmlns/os "" :xmlns/xsi "" :xmlns ""})
generates a
IllegalArgumentException No value supplied for key: {:xmlns/gh "", :xmlns/gs "", :xmlns/os "", :xmlns/xsi "", :xmlns ""}  clojure.lang.PersistentHashMap.create (PersistentHashMap.java:77)
exception 😕

dominicm13:02:10

@U0DPX8ZQB The way I think about zippers is that they're about taking a data structure (e.g. {:foo {:bar 1}}) and adding a location (`[:foo]`). You can get the (value) at the current point (which would be {:bar 1}), and you can "move", so do things like (down) which would change your position to [:foo :bar] and (value) would be 1, or you could go (up) to [] and the value would be {:foo {:bar 1}}

dominicm13:02:21

@paulspendI don't know much about the namespacing system in clojure.data.xml, but that doesn't look like how we use it.

dominicm13:02:08

Ours looks more like ::gh/foobar, but I'm not certain 🙂

dominicm13:02:19

@U064B4L0K Oh, the API changed recently, so that's probably what you're running into.

paulspencerwilliams13:02:25

Gotcha, I saw a sample on the readme with ‘{:xmlns/foo "http://www.w3.org/199’ but it was for xml, not emit-str opts!

paulspencerwilliams07:02:40

Morning btw, how rude 😉

jarohen09:02:44

morning 🙂

chrjs10:02:39

Morning

nigel13:02:59

afternoon 🙂

paulspencerwilliams13:02:18
replied to a thread:So I've enjoyed my foray into zippers this week. I wanted to make a small change to a single value in ~ 100 xml files and reading / parsing / zipping xml (clojure.zip/xml-zip (clojure.data.xml/parse (<http://clojure.java.io/file|clojure.java.io/file> "path-to-data.xml"))) worked a treat. Using zip/down, and zip/edit was also pretty easy to grok. Now I just needed to write back the file. Simple, just invoke `clojure.data.xml/emit` on the zip? Yes, on simple xml. However my gpx contains xml and `emit` fails with XMLStreamException Prefix cannot be null com.sun.xml.internal.stream.writers.XMLStreamWriterImpl.writeAttribute (XMLStreamWriterImpl.java:575) probably related to https://groups.google.com/forum/#!topic/clojure/Um3aC_z0--4 Am I being stupid? Should I fall back to clojure.xml? How would you approach this seemingly simple task? I ask as most googling of zippers + xml return pages circa 2014 so am I just doing this an old fashioned error-prone or non-idiomatic way?

So, without attempting to apply namespaces, emit-str understandable generates a

ExceptionInfo Auto-generating prefixes is not supported for content-qnames. Please declare all URIs used in content qnames.  clojure.core/ex-info (core.clj:4617)
, and adding all the namespaces for
gpx xmlns:gh="" xmlns:gs="" xmlns:os="" xmlns:xsi="" xmlns="" creator="OSMaps" version="1.1" xsi:schemaLocation="">
like
(xml/emit-str (first (edit-title (second all-files))) {:xmlns/gh "" :xmlns/gs "" :xmlns/os "" :xmlns/xsi "" :xmlns ""})
generates a
IllegalArgumentException No value supplied for key: {:xmlns/gh "", :xmlns/gs "", :xmlns/os "", :xmlns/xsi "", :xmlns ""}  clojure.lang.PersistentHashMap.create (PersistentHashMap.java:77)
exception 😕

sundarj20:02:32

got told today that the company i work for is going to be moving away from doing development work in March (which is fair enough). i haven't really been enjoying my work for a while now, and had recently been thinking of getting around to updating my CV and finding some new work, so i'm not too disappointed (though i will miss my colleagues) - but now i have until March to 🙂

sundarj20:02:32

i don't enjoy writing CVs, but guess i'll make a start on it tomorrow 😛

reborg21:02:55

ouch @sundarj sorry to hear that. But keep thinking positive, the next job will be much better 🙂

sundarj21:02:01

@reborg thanks! i'm counting on it 😁

sundarj21:02:02

for now i'm watching star wars and sipping a glass of gin and fanta (we're out of tonic)