Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure: data.xml: create key named "xlink:href"

Context

I'm outputting a xml file from clojure using data.xml. I need to output the following line:

<use xlink:href="#whiskers" transform="scale(-1 1) translate(-140 0)" />

(We're drawing a cat in SVG).

Now, my try is something like:

(indent-str (element :use {:xlink:href "#whiskers",
               :transform "scale(-1 1) translate(-140 0)"}))

This fails because :xlink:href "#whiwksers" is apparently interpreted as: :xlink , :href "#whiskers"

Question

How do I create a clojure symbol to output "xlink:href" as a field for data.xml?

Edit

I have tried: (keyword "xlink:href")

still same error. Not sure what's going on.


1 Answers

I'm not sure there is an error here.

(def e (element :use {:xlink:href "#whiskers",
          :transform "scale(-1 1) translate(-140 0)"}))

(println (emit-str e))
=> <?xml version="1.0" encoding="UTF-8"?><use xlink:href="#whiskers" transform="scale(-1 1) translate(-140 0)"></use>

The indent-str function has a problem though:

(println (indent-str e))
org.xml.sax.SAXParseException: The prefix "xlink" for attribute "xlink:href" 
associated with an element type "use" is not bound.

So it appears clojure.data.xml is namespace aware. Let's try this:

(def e-the-sequel (element :use {:xmlns:xlink "http://testing", 
                                 :xlink:href "#whiskers",
                                 :transform "scale(-1 1) translate(-140 0)"}))

(println (indent-str e-the-sequel))
=> <?xml version="1.0" encoding="UTF-8"?>
<use xmlns:xlink="http://testing" xlink:href="#whiskers" 
     transform="scale(-1 1) translate(-140 0)"/>

That's better. If anything, I guess you could argue that emit-str is wrong because it didn't throw an exception.

like image 199
stand Avatar answered May 23 '26 18:05

stand