Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 3 commits
  • 3 files changed
  • 4 commit comments
  • 1 contributor
@@ -97,6 +97,13 @@ The same can also be expressed using a more Hiccup-like style of defining the el
[:baz {} "The baz value"]]]))
;;-> true
+Comments and CData can also be emitted as an S-expression with the special tag names :-cdata and :-comment:
+ (= (element :tag {:attr "value"}
+ (element :body {} (cdata "not parsed <stuff")))
+ (sexp-as-element [:tag {:attr "value"} [:body {} [:-cdata "not parsed <stuff"]]]
+ ;;-> true
XML can be "round tripped" through the library:
(let [tags (element :foo {:foo-attr "foo value"}
8 src/main/clojure/clojure/data/xml.clj
@@ -178,6 +178,12 @@
(defprotocol AsElements
(as-elements [expr] "Return a seq of elements represented by an expression."))
+(defn sexp-element [tag attrs child]
+ (cond
+ (= :-cdata tag) (CData. (first child))
+ (= :-comment tag) (Comment. (first child))
+ :else (Element. tag attrs (mapcat as-elements child))))
(extend-protocol AsElements
(as-elements [v]
@@ -187,7 +193,7 @@
[k (str v)]))
[{} content])]
- [(Element. tag attrs (mapcat as-elements content))]))
+ [(sexp-element tag attrs content)]))
(as-elements [s]
25 src/test/clojure/clojure/data/xml/test_sexp.clj
@@ -25,3 +25,28 @@
(is (= (sexps-as-fragment input)
(map sexp-as-element input)))
(is (thrown? Exception (sexp-as-element input)))))
+(deftest with-cdata
+ (let [xml-input (element :tag {:attr "value"}
+ (element :body {} (cdata "not parsed <stuff")))
+ sexp-input [:tag {:attr "value"} [:body {} [:-cdata "not parsed <stuff"]]]]
+ (is (= xml-input
+ (sexp-as-element sexp-input)))))
+(deftest with-multiple-cdata
+ (let [xml-input (element :tag {:attr "value"}
+ (element :body {}
+ (cdata "not parsed <stuff")
+ (cdata "more not parsed <stuff")))
+ sexp-input [:tag {:attr "value"} [:body {}
+ (list [:-cdata "not parsed <stuff"]
+ [:-cdata "more not parsed <stuff"])]]]
+ (is (= xml-input
+ (sexp-as-element sexp-input)))))
+(deftest with-comment
+ (let [xml-input (element :tag {:attr "value"}
+ (element :body {} (xml-comment "comment <stuff<here<")))
+ sexp-input [:tag {:attr "value"} [:body {} [:-comment "comment <stuff<here<"]]]]
+ (is (= xml-input
+ (sexp-as-element sexp-input)))))

Showing you all comments on commits in this comparison.


Are these changes not included in 0.0.6? I think I just spent an hour trying to understand why my cdata's weren't parsing as I hoped. :)

What are the chances we could get a release of 0.0.7 soon? We need this functionality at work. What can I do to move it along? I have signed a CA.


They are not. I'll get 0.0.7 released today and let you know when it's out there.


0.0.7 has been released

Something went wrong with that request. Please try again.