Below roundtrip produces invaild xml as the result is not escaped correctly, i.e. the attribute values contain ‘ instead of apos;. Am I doing somthing wrong or is this a bug?
(ns xml-test
(:require [clojure.xml :as xml])
(:require [clojure.zip :as zip]))
(def test-xml "<?xml version="1.0" encoding="UTF-8"?> <main> <item attr=''test''> </item> </main>")
(def s (ByteArrayInputStream. (.getBytes test-xml "UTF-8")))
(xml/emit (zip/root (zip/xml-zip (clojure.xml/parse s))))
output:
<?xml version='1.0' encoding='UTF-8'?>
<main>
<item attr=''test''/>
</main>
nil
I’ve checked the source quickly and
clojure.xml/emit-element(which gets called byclojure.xml/emit) makes no effort whatever to encode any characters as XML entities; in fact, it lets attribute values straight through. I guess this meansclojure.xmlis quite limited in its usability; you should useclojure.contrib.lazy-xmlinstead. My apologies for not mentioning it in the answer to your first question on XML emitting, I didn’t realise stuff like this would happen.With
clojure.contrib.lazy-xml, you can do the following:If you really wanted to use
clojure.xml, you’d have to pass onclojure.xml/emitand use an XML producer of your choice instead. Well, actually, you can useclojure.xml/parse, mangle the result, then pass it toclojure.contrib.lazy-xml/emit; the structure of the Clojure representation of the XML is the same with both libraries, but only the latter does proper emitting.