lib/xommelier/xml/element/serialization.rb in xommelier-0.1.4 vs lib/xommelier/xml/element/serialization.rb in xommelier-0.1.5

- old
+ new

@@ -62,31 +62,40 @@ options = SERIALIZATION_OPTIONS.merge(options) element_name = options.delete(:element_name) { self.element_name } if options[:builder] # Non-root element builder = options.delete(:builder) attribute_values = {} + namespaces = builder.doc.namespaces prefix = builder.doc.namespaces.key(xmlns.uri)[6..-1].presence else # Root element builder = Nokogiri::XML::Builder.new(options) attribute_values = children_namespaces.inject({xmlns: xmlns.uri}) do |hash, ns| hash["xmlns:#{ns.as}"] = ns.uri hash end attribute_values.delete("xmlns:#{xmlns.as.to_s}") + attribute_values.delete("xmlns:xml") + namespaces = attribute_values prefix = nil end current_xmlns = builder.doc.namespaces[prefix ? "xmlns:#{prefix}" : 'xmlns'] attributes.each do |name, value| - if (ns = self.class.attributes[name][:ns]).uri != current_xmlns && attr_prefix = builder.doc.namespaces.key(ns.uri).try(:[], 6..-1).presence - name = "#{attr_prefix}:#{name}" + attribute_options = attribute_options(name) + attribute_name = attribute_options[:attribute_name] + if (ns = attribute_options[:ns]).uri != current_xmlns + if ns.as == :xml + attribute_name = "xml:#{attribute_options[:attribute_name]}" + elsif attr_prefix = namespaces.key(ns.uri).try(:[], 6..-1).presence + attribute_name = "#{attr_prefix}:#{attribute_options[:attribute_name]}" + end end - serialize_attribute(name, value, attribute_values) + serialize_attribute(attribute_name, value, attribute_values) end (prefix ? builder[prefix] : builder). send(element_name, attribute_values) do |xml| elements.each do |name, value| - serialize_element(name, value, xml, self.class.elements[name].merge(parent_ns_prefix: prefix)) + serialize_element(name, value, xml, element_options(name).merge(parent_ns_prefix: prefix)) end if respond_to?(:text) xml.text @text end end @@ -102,11 +111,14 @@ def children_namespaces(namespaces = Set[xmlns]) elements.inject(namespaces) do |result, (name, children)| element_options = self.class.elements[name] result << element_options[:ns] + result += attributes.keys.map { |name| attribute_options(name)[:ns] } if element_options[:type] < Xml::Element - Array(children).each { |child| result += child.children_namespaces } + Array(children).each do |child| + result += child.children_namespaces + end end result end end