Reputation: 1720
When generating a XML with org.clojure/data.xml with the latest stable version 0.0.8, we get a non-namespaced XML back. When generating a XML with the latest preview release 0.2.0-alpha6, it generates a prefixed XML.
According to the libraries documentation, namespace support is a new feature. However, how is possible to generate a XML without a namespace? For some XMLs, like Atom, this is a requirement for a valid XML.
Here's the code that works on both versions:
(xml/emit-str
(xml/sexp-as-element
[:feed {:xmlns "http://www.w3.org/2005/Atom"}]))
In stable, it generates:
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><feed xmlns=\"http://www.w3.org/2005/Atom\"/>"
In the preview release, it generates:
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><feed xmlns:a=\"http://www.w3.org/2005/Atom\"/>"
Notice the auto-generated :a
namespace prefix in the latter.
Update: alex-miller proposed a solution to the above problem in https://stackoverflow.com/a/64615916/252585
I would very much like to accept this answer, because it seems to solve what I've asked. However, for the sake of getting a valid Atom feed, it pushes the problem further down the line. I've looked at the documentation with the new information on using an URI alias, but haven't been successful in generating one. Here's the resulting new problem with the proposed approach:
The stable version of clojure.data.xml produces:
(xml/emit-str
(xml/sexp-as-element
[:feed {:xmlns "http://www.w3.org/2005/Atom"}
[:title "Some site title"]]))
=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?><feed xmlns:a=\"http://www.w3.org/2005/Atom\"><title>Some site title</title></feed>"
The latest version with the new approach produces:
(xml/emit-str
(xml/sexp-as-element
[::atom/feed {:xmlns "http://www.w3.org/2005/Atom"}
[:title "Some site title"]]))
=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?><feed xmlns=\"http://www.w3.org/2005/Atom\"><title xmlns=\"\" xmlns:a=\"http://www.w3.org/2005/Atom\">Some site title</title></feed>"
This syntax, will again be rejected by Atom validators and feed readers. Here is an example: https://user-images.githubusercontent.com/505721/97803188-bbdd5600-1c48-11eb-986a-14d441344e70.png
Upvotes: 3
Views: 210
Reputation: 70211
I think you can get what you want like this using uri aliases:
(require '[clojure.data.xml :as xml])
;; declare a uri alias in this namespace
(xml/alias-uri 'atom "http://www.w3.org/2005/Atom")
;; use the alias as tag prefix, but also declare at the root as default
(xml/emit-str
(xml/sexp-as-element
[::atom/feed {:xmlns "http://www.w3.org/2005/Atom"}]))
;; emitted string:
;; "<?xml version=\"1.0\" encoding=\"UTF-8\"?><feed xmlns=\"http://www.w3.org/2005/Atom\"/>"
Upvotes: 4