Skip to content

Commit 3a6e66d

Browse files
committed
add example using xmlns and emitted unqualified tags
1 parent 0c3a9e0 commit 3a6e66d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ Below is an example of parsing an XHTML document:
226226

227227
Emitting namespaced XML is usually done by using `alias-uri` in combination with clojure's built-in `::kw-ns/shorthands`:
228228

229+
;; this needs to be at the top level of your code (parallel to defns)
230+
;; or subsequent ::xh/ ... will throw "Invalid token"
229231
(xml/alias-uri 'xh "http://www.w3.org/1999/xhtml")
230232

231233
(xml/emit-str {:tag ::xh/html
@@ -237,6 +239,35 @@ Emitting namespaced XML is usually done by using `alias-uri` in combination with
237239
<a:body>DOCUMENT</a:body>
238240
</a:html>
239241

242+
To emit namespaced tags without prefixes, you can also set the default xmlns at the root (it's important that the uris match!!):
243+
244+
;; at top level
245+
(xml/alias-uri 'xh "http://www.w3.org/1999/xhtml")
246+
247+
;; top-level element should set xmlns that matches
248+
(xml/emit-str
249+
(xml/element ::xh/html
250+
{:xmlns "http://www.w3.org/1999/xhtml"}
251+
(xml/element ::xh/head)
252+
(xml/element ::xh/body {} "DOCUMENT")))
253+
254+
;; newlines and indents added for readability, not in actual output
255+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
256+
<html xmlns=\"http://www.w3.org/1999/xhtml\"><head/>
257+
<body>DOCUMENT</body>
258+
</html>"
259+
260+
Same example, but using the more concise hiccup style (same output):
261+
262+
;; at top level
263+
(xml/alias-uri 'xh "http://www.w3.org/1999/xhtml")
264+
265+
(xml/emit-str
266+
(xml/sexp-as-element
267+
[::xh/html {:xmlns "http://www.w3.org/1999/xhtml"}
268+
[::xh/head]
269+
[::xh/body "DOCUMENT"]]))
270+
240271
It is also allowable to use `javax.xml.namespace.QName` instances, as well as strings with the informal `{ns}n` encoding.
241272

242273
(xml/emit-str {:tag (xml/qname "http://www.w3.org/1999/xhtml" "html")})

0 commit comments

Comments
 (0)