 
 (extend-protocol XMLWritableAttr
   clojure.lang.Keyword
-  (emit-attr [kwd val ^Writer out]
+  (emit-attr [kwd val out]
+    (emit-attr (name kwd) val out))
+
+  String
+  (emit-attr [name val ^Writer out]
     (doto out
-      (.write (name kwd))
-      (.write "=\"")
+      (.write name)
+      (.write "='")
       (.write (xml-escape-attr (str val)))
-      (.write \")))
+      (.write "'")))
 
   clojure.lang.IPersistentVector
-  (emit-attr [[name uri] ]))
-
-(defn emit-element [^Element e ^Writer out]
-  (let [tag (tag e)
-        attrs (attrs e)
-        content (content e)
-        prefix (prefix e)
+  (emit-attr [[kwd uri :as attr] val ^Writer out]
+    (if uri
+      (if-let [prefix (:prefix (meta attr))]
+        (emit-attr (str prefix  \: (name kwd)) val out))
+      (emit-attr kwd val out))))
+
+(defn emit-element
+  [elt ^Writer out]
+  (let [tag (tag elt)
+        attrs (attrs elt)
+        content (content elt)
+        prefix (prefix-for elt)
         tagname (str prefix (if prefix \:) (name tag))]
     (doto out
       (.write "<")
       (.write tagname))
     (doseq [[k v] attrs]
       (when-not (nil? v)
-        (.write " ")
+        (.write out " ")
         (emit-attr k v out)))
     (if (empty? content)
       (.write out "/>")



(defn attrs->map
  [^Attributes attrs namespaces]
  (let [attr-key
        (if namespaces
          (fn [^Attributes attrs ^Long i]
            (let [uri (.getURI attrs i)
                  q-name (.getQName attrs i)]

  (let [{:keys [target text]} pi]
    ;; TODO - validate target: http://www.w3.org/TR/REC-xml/#sec-pi
    (when (.contains text "?>")
      (throw (IllegalArgumentException. "Illegal end sequence, \"?>\", in processing instruction.")))
    (doto out
      (.write "<?")
      (.write target)
