require 'nokogiri' def validate_xml_against_xsd(xml, xsd) require 'tempfile' tempfile = Tempfile.new('schematic') tempfile << xsd tempfile.rewind xsd = Nokogiri::XML::Schema(tempfile) doc = Nokogiri::XML.parse(xml) errors = [] xsd.validate(doc).each do |error| errors << error.message end expect(errors).to eq([]) ensure tempfile.close end def validate_xsd(xml) xsd_schema_file = File.join(File.dirname(__FILE__), 'xsd', 'XMLSchema.xsd') meta_xsd = Nokogiri::XML::Schema(File.open(xsd_schema_file)) doc = Nokogiri::XML.parse(xml) meta_xsd.validate(doc).each do |error| expect(error.message).to be_nil end end def sanitize_xml(xml) xml.split("\n").reject(&:blank?).map(&:strip).join("\n") end def generate_xsd_for_model(model, header_element = nil) xsd_generator = model.schematic_sandbox.xsd_generator output = <<-XML #{header_element} #{yield} XML sanitize_xml(output) end