 
         container = @$xml.find('metadata')
         selector = tagName.replace(':', '\\:')
-        template = "<#{tagName}"
+
+        element = @$xml[0].createElement(tagName)
 
         _.forIn attributes, (value, key) ->
-          selector += "[#{key}=\"#{value}\"]"
-          template += " #{key}=\"#{value}\""
-
-        @$xml.find(selector).remove()
-        template += "></#{tagName}>"
+          selector += "[#{key.replace(':', '\\:')}=\"#{value}\"]"
+          element.setAttributeNS('http://www.idpf.org/2007/opf', key, value)
+
+        container.find(selector).remove()
 
         container.append('    ')
-        $(template).text(value).appendTo(container)
+        element.textContent = value
+        container[0].appendChild(element)
         container.append('\n')
         if not skipSave
           setNavModel({})

          fileAs = creator.split(/[ ](?=[^ ]+$)/).reverse().join(', ')

          container.append('    ')
          newCreator = $(template).text(creator)
            .attr('id', id)
            .attr('file-as', fileAs)
            .appendTo(container)
          container.append('\n    ')
          $(refinesTemplate)
            .attr('refines', '#' + id)


        container = @$xml.find('metadata')
        selector = tagName.replace(':', '\\:')
        template = @$xml[0].createElement(tagName)
 
        _.forIn attributes, (value, key) ->
          selector += "[#{key.replace(':', '\\:')}=\"#{value}\"]"
          template.setAttributeNS('http://www.idpf.org/2007/opf', key, value)

        existing = @$xml.find(selector).remove()

        _.each values, (value) ->
          element = template.cloneNode()
          container.append('    ')
          element.textContent = value
          container[0].appendChild(element)
          container.append('\n')

        setNavModel({})
