Module: Transformator::FormatConverter::DocumentFromXml

Included in:
Transformator::FormatConverter, DocumentFromHash
Defined in:
lib/transformator/format_converter/document_from_xml.rb

Instance Method Summary collapse

Instance Method Details

#document_from_xml(xml, options = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/transformator/format_converter/document_from_xml.rb', line 2

def document_from_xml(xml, options = {})
  xml =
  if options[:remove_whitespace_only_text_nodes] || options[:remove_namespaces]
    xml.dup
  else
    xml
  end

  unless options[:remove_whitespace_only_text_nodes] == false
    Transformator::FormatConverter.remove_whitespace_only_text_nodes!(xml)
  end

  if options[:remove_namespaces] == true
    Transformator::FormatConverter.remove_namespaces!(xml)
  end

  if xml[/\A\s*<\?xml/]
    Ox.parse(xml)
  else
    Ox::Document.new(version: "1.0", encoding: "UTF-8").tap do |new_document|
      Ox.parse("<root>" << xml << "</root>").nodes.each do |node|
        new_document << node
      end
    end
  end
end