Module: OpenAgent::XMLHelpers

Included in:
Client
Defined in:
lib/openagent/xml_helpers.rb

Constant Summary collapse

PP_XSLT =
File.read(File.join(File.dirname(__FILE__), 'pretty_print.xslt'))
PP_XSL =
Nokogiri::XSLT(PP_XSLT)

Instance Method Summary collapse

Instance Method Details

#formatted_xml(xml, pretty_print = true) ⇒ Object



8
9
10
# File 'lib/openagent/xml_helpers.rb', line 8

def formatted_xml(xml, pretty_print=true)
  pretty_print ? parse_well_formed_xml(xml) : xml
end

#parse_well_formed_xml(xml) ⇒ Object

If XML is well-formed, reformat it as a pretty XML document.



17
18
19
20
21
22
# File 'lib/openagent/xml_helpers.rb', line 17

def parse_well_formed_xml(xml)
  doc = Nokogiri::XML(xml){ |c| c.strict }
  pretty_xml(doc)
rescue Nokogiri::XML::SyntaxError
  xml
end

#pretty_xml(doc) ⇒ Object



12
13
14
# File 'lib/openagent/xml_helpers.rb', line 12

def pretty_xml(doc)
  PP_XSL.apply_to(doc).to_s
end