@@ -78,9 +78,9 @@ org.clojure/data.xml {:mvn/version "0.2.0-alpha8"}
7878
7979## Examples
8080
81- The examples below assume you have added a ` :refer :all ` for data.xml:
81+ The examples below assume you have added a ` :refer ` for data.xml:
8282
83- (require '[clojure.data.xml :refer :all ])
83+ (require '[clojure.data.xml :as xml ])
8484
8585data.xml supports parsing and emitting XML. The parsing functions will
8686read XML from a
9090
9191 (let [input-xml (java.io.StringReader. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
9292 <foo><bar><baz>The baz value</baz></bar></foo>")]
93- (parse input-xml))
93+ (xml/ parse input-xml))
9494
9595 #clojure.data.xml.Element{:tag :foo,
9696 :attrs {},
@@ -104,69 +104,69 @@ The data is returned as defrecords and can be manipulated using the
104104normal clojure data structure functions. Additional parsing options
105105can be passed via key pairs:
106106
107- (parse-str "<a><![CDATA[\nfoo bar\n]]><![CDATA[\nbaz\n]]></a>" :coalescing false)
107+ (xml/ parse-str "<a><![CDATA[\nfoo bar\n]]><![CDATA[\nbaz\n]]></a>" :coalescing false)
108108 #clojure.data.xml.Element{:tag :a, :attrs {}, :content ("\nfoo bar\n" "\nbaz\n")}
109109
110110XML elements can be created using the typical defrecord constructor
111111functions or the element function used below or just a plain map with : tag : attrs : content keys, and written using a
112112[ java.io.Writer] ( https://docs.oracle.com/javase/8/docs/api/java/io/Writer.html ) .:
113113
114- (let [tags (element :foo {:foo-attr "foo value"}
115- (element :bar {:bar-attr "bar value"}
116- (element :baz {} "The baz value")))]
114+ (let [tags (xml/ element :foo {:foo-attr "foo value"}
115+ (xml/ element :bar {:bar-attr "bar value"}
116+ (xml/ element :baz {} "The baz value")))]
117117 (with-open [out-file (java.io.FileWriter. "/tmp/foo.xml")]
118- (emit tags out-file)))
118+ (xml/ emit tags out-file)))
119119
120120 ;;-> Writes XML to /tmp/foo.xml
121121
122122The same can also be expressed using a more Hiccup-like style of defining the elements using sexp-as-element:
123123
124- (= (element :foo {:foo-attr "foo value"}
125- (element :bar {:bar-attr "bar value"}
126- (element :baz {} "The baz value")))
127- (sexp-as-element
124+ (= (xml/ element :foo {:foo-attr "foo value"}
125+ (xml/ element :bar {:bar-attr "bar value"}
126+ (xml/ element :baz {} "The baz value")))
127+ (xml/ sexp-as-element
128128 [:foo {:foo-attr "foo value"}
129129 [:bar {:bar-attr "bar value"}
130130 [:baz {} "The baz value"]]]))
131131 ;;-> true
132132
133133Comments and CDATA can also be emitted as an S-expression with the special tag names :-cdata and :-comment:
134134
135- (= (element :tag {:attr "value"}
136- (element :body {} (cdata "not parsed <stuff")))
137- (sexp-as-element [:tag {:attr "value"} [:body {} [:-cdata "not parsed <stuff"]]]
135+ (= (xml/ element :tag {:attr "value"}
136+ (xml/ element :body {} (xml/ cdata "not parsed <stuff")))
137+ (xml/ sexp-as-element [:tag {:attr "value"} [:body {} [:-cdata "not parsed <stuff"]]]
138138 ;;-> true
139139
140140XML can be "round tripped" through the library:
141141
142- (let [tags (element :foo {:foo-attr "foo value"}
143- (element :bar {:bar-attr "bar value"}
144- (element :baz {} "The baz value")))]
142+ (let [tags (xml/ element :foo {:foo-attr "foo value"}
143+ (xml/ element :bar {:bar-attr "bar value"}
144+ (xml/ element :baz {} "The baz value")))]
145145 (with-open [out-file (java.io.FileWriter. "/tmp/foo.xml")]
146- (emit tags out-file))
146+ (xml/ emit tags out-file))
147147 (with-open [input (java.io.FileInputStream. "/tmp/foo.xml")]
148- (parse input)))
148+ (xml/ parse input)))
149149
150150 #clojure.data.xml.Element{:tag :foo, :attrs {:foo-attr "foo value"}...}
151151
152152There are also some string based functions that are useful for
153153debugging.
154154
155- (let [tags (element :foo {:foo-attr "foo value"}
156- (element :bar {:bar-attr "bar value"}
157- (element :baz {} "The baz value")))]
158- (= tags (parse-str (emit-str tags))))
155+ (let [tags (xml/ element :foo {:foo-attr "foo value"}
156+ (xml/ element :bar {:bar-attr "bar value"}
157+ (xml/ element :baz {} "The baz value")))]
158+ (= tags (xml/ parse-str (xml/ emit-str tags))))
159159
160160 true
161161
162162Indentation is supported, but should be treated as a debugging feature
163163as it's likely to be pretty slow:
164164
165- (print (indent-str (element :foo {:foo-attr "foo value"}
166- ( element :bar {:bar-attr "bar value"}
167- ( element :baz {} "The baz value1")
168- ( element :baz {} "The baz value2")
169- ( element :baz {} "The baz value3")))))
165+ (print (xml/ indent-str (xml/ element :foo {:foo-attr "foo value"}
166+ (xml/ element :bar {:bar-attr "bar value"}
167+ (xml/ element :baz {} "The baz value1")
168+ (xml/ element :baz {} "The baz value2")
169+ (xml/ element :baz {} "The baz value3")))))
170170
171171 <?xml version="1.0" encoding="UTF-8"?>
172172 <foo foo-attr="foo value">
@@ -179,33 +179,34 @@ as it's likely to be pretty slow:
179179
180180CDATA can be emitted:
181181
182- (emit-str (element :foo {}
183- ( cdata "<non><escaped><info><here>")))
182+ (xml/ emit-str (xml/ element :foo {}
183+ (xml/ cdata "<non><escaped><info><here>")))
184184
185185 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo><![CDATA[<non><escaped><info><here>]]></foo>"
186186
187187But will be read as regular character data:
188188
189- (parse-str (emit-str (element :foo {}
190- ( cdata "<non><escaped><info><here>"))))
189+ (xml/ parse-str (xml/ emit-str (element :foo {}
190+ (xml/ cdata "<non><escaped><info><here>"))))
191191
192192 #clojure.data.xml.Element{:tag :foo, :attrs {}, :content ("<non><escaped><info><here>")}
193193
194194Comments can also be emitted:
195195
196- (emit-str (element :foo {}
197- (xml-comment "Just a <comment> goes here")
198- (element :bar {} "and another element")))
196+ (xml/emit-str
197+ (xml/element :foo {}
198+ (xml/xml-comment "Just a <comment> goes here")
199+ (xml/element :bar {} "and another element")))
199200
200201 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo><!--Just a <comment> goes here--><bar>and another element</bar></foo>"
201202
202203But are ignored when read:
203204
204- (emit-str
205- (parse-str
206- (emit-str (element :foo {}
207- ( xml-comment "Just a <comment> goes here")
208- ( element :bar {} "and another element")))))
205+ (xml/ emit-str
206+ (xml/ parse-str
207+ (xml/ emit-str (xml/ element :foo {}
208+ (xml/ xml-comment "Just a <comment> goes here")
209+ (xml/ element :bar {} "and another element")))))
209210
210211 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo><bar>and another element</bar></foo>"
211212
@@ -215,19 +216,19 @@ XML Namespaced names (QNames) are encoded into clojure keywords, by percent-enco
215216
216217Below is an example of parsing an XHTML document:
217218
218- (parse-str "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
219- <foo:html xmlns:foo=\"http://www.w3.org/1999/xhtml\"/>")
219+ (xml/ parse-str "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
220+ <foo:html xmlns:foo=\"http://www.w3.org/1999/xhtml\"/>")
220221
221222 #...Element{:tag :xmlns.http%3A%2F%2Fwww.w3.org%2F1999%2Fxhtml/html,
222223 :attrs {},
223224 :content ()}
224225
225226Emitting namespaced XML is usually done by using ` alias-uri ` in combination with clojure's built-in ` ::kw-ns/shorthands ` :
226227
227- (alias-uri 'xh "http://www.w3.org/1999/xhtml")
228+ (xml/ alias-uri 'xh "http://www.w3.org/1999/xhtml")
228229
229- (emit-str {:tag ::xh/html
230- :content [{:tag ::xh/head} {:tag ::xh/body :content ["DOCUMENT"]}]})
230+ (xml/ emit-str {:tag ::xh/html
231+ :content [{:tag ::xh/head} {:tag ::xh/body :content ["DOCUMENT"]}]})
231232
232233 <?xml version="1.0" encoding="UTF-8"?>
233234 <a:html xmlns:a="http://www.w3.org/1999/xhtml">
@@ -237,8 +238,8 @@ Emitting namespaced XML is usually done by using `alias-uri` in combination with
237238
238239It is also allowable to use ` javax.xml.namespace.QName ` instances, as well as strings with the informal ` {ns}n ` encoding.
239240
240- (emit-str {:tag (qname "http://www.w3.org/1999/xhtml" "html")})
241- (emit-str {:tag "{http://www.w3.org/1999/xhtml}html"})
241+ (xml/ emit-str {:tag (qname "http://www.w3.org/1999/xhtml" "html")})
242+ (xml/ emit-str {:tag "{http://www.w3.org/1999/xhtml}html"})
242243
243244 <?xml version=\"1.0\" encoding=\"UTF-8\"?><a:html xmlns:a=\"http://www.w3.org/1999/xhtml\"></a:html>
244245
@@ -248,41 +249,44 @@ Prefixes are mostly an artifact of xml serialisation. They can be
248249customized by explicitly declaring them as attributes in the ` xmlns `
249250kw-namespace:
250251
251- (emit-str (element (qname "http://www.w3.org/1999/xhtml" "title")
252- {:xmlns/foo "http://www.w3.org/1999/xhtml"}
253- "Example title"))
252+ (xml/emit-str
253+ (xml/element (xml/qname "http://www.w3.org/1999/xhtml" "title")
254+ {:xmlns/foo "http://www.w3.org/1999/xhtml"}
255+ "Example title"))
254256 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo:title xmlns:foo=\"http://www.w3.org/1999/xhtml\">Example title</foo:title>"
255257
256258Not specifying a namespace prefix will results in a prefix being generated:
257259
258- (emit-str (element ::xh/title
259- {}
260- "Example title"))
260+ (xml/emit-str
261+ (xml/element ::xh/title
262+ {}
263+ "Example title"))
261264
262265 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><a:title xmlns:a=\"http://www.w3.org/1999/xhtml\">Example title</a:title>"
263266
264267The above example auto assigns prefixes for the namespaces used. In
265268this case it was named ` a ` by the emitter. Emitting several nested
266269tags with the same namespace will use one prefix:
267270
268- (emit-str (element ::xh/html
269- {}
270- (element ::xh/head
271+ (xml/emit-str
272+ (xml/element ::xh/html
273+ {}
274+ (xml/element ::xh/head
271275 {}
272- (element ::xh/title
273- {}
274- "Example title"))))
276+ (xml/ element ::xh/title
277+ {}
278+ "Example title"))))
275279
276280 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><a:html xmlns:a=\"http://www.w3.org/1999/xhtml\"><a:head><a:title>Example title</a:title></a:head></a:html>"
277281
278282Note that the jdk QName ignores namespace prefixes for equality, but allows to preserve them for emitting.
279283
280- (= (parse-str "<foo:title xmlns:foo=\"http://www.w3.org/1999/xhtml2\">Example title</foo:title>")
281- (parse-str "<bar:title xmlns:bar=\"http://www.w3.org/1999/xhtml2\">Example title</bar:title>"))
284+ (= (xml/ parse-str "<foo:title xmlns:foo=\"http://www.w3.org/1999/xhtml2\">Example title</foo:title>")
285+ (xml/ parse-str "<bar:title xmlns:bar=\"http://www.w3.org/1999/xhtml2\">Example title</bar:title>"))
282286
283287In data.xml prefix mappings are (by default) retained in metadata on a tag record. If there is no metadata, new prefixes will be generated when emitting.
284288
285- (emit-str (parse-str "<foo:element xmlns:foo=\"FOO:\" />"))
289+ (xml/ emit-str (xml/ parse-str "<foo:element xmlns:foo=\"FOO:\" />"))
286290
287291## Location information as meta
288292
@@ -293,11 +297,11 @@ By default the parser attaches location information as element meta,
293297 (deftest test-location-meta
294298 (let [input "<a><b/>\n<b/></a>"
295299 location-meta (comp :clojure.data.xml/location-info meta)]
296- (is (= 1 (-> input parse-str location-meta :line-number)))
300+ (is (= 1 (-> input xml/ parse-str location-meta :line-number)))
297301
298302To elide location information, pass ` :location-info false ` to the parser:
299303
300- (parse-str your-input :location-info false)
304+ (xml/ parse-str your-input :location-info false)
301305
302306## Clojurescript support
303307
0 commit comments