Batik, SVG and weird javascript errors

I am using Clojure to generate SVG documents. To do this I use a library called lacij which in turn uses tikkba; a wrapper around the Apache Batik SVG generation library.

I added support for embedding javascript inside the SVG document (not the html) by manipulating the DOM as described here http://stackoverflow.com/questions/17906992/adding-script-to-lacij-graph

The problem was that I was getting weird svg error “unknown language text/ecmascript”. As it turns out I was using the wrong xlink namespace and posted the answer on the above mentioned thread.

The code I wrote now is like this:

(defn build-svg-graph [graph]
(def new-graph(-> graph
(LALayout/layout :hierarchical)
(LAGraph/build)
)
)

(def element
(Dom/elements
(:xmldoc new-graph)
org.apache.batik.util.XMLConstants/XLINK_NAMESPACE_URI
[:script {"xlink:href" "myscript.js" :type "text/ecmascript"}]
))

(Dom/append-child (Dom/document-element (:xmldoc new-graph)) element)

(Dom/spit-str (:xmldoc new-graph) :indent "yes")
)

Note that I needed to use xlink:href instead of src for embedding javascript refs in svg. src works in HTML not in SVG

Advertisements

One response to “Batik, SVG and weird javascript errors

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s