public
Created

  • Download Gist
XML Transformer Challenge
1 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
require 'rubygems'
require 'nokogiri'
#
class Challenge
FIRST_NAME = %w{first, first_name }
LAST_NAME = %w{last, last_name, surname}
def initialize(filename)
@input_file = filename
end
def parse
f = File.open(@input_file)
input_doc = Nokogiri::XML(f)
f.close
people_doc = nil
if File.exists?("result.xml")
f = File.open("result.xml")
people_doc = Nokogiri::XML(f)
f.close
else
people_doc = Nokogiri::XML("<people/>")
end
people = people_doc.at_css('people')
input_doc.css(FIRST_NAME.to_s).each do |node|
first= Nokogiri::XML::Node.new('first', people_doc)
first.content = node.content
lastname = node.parent.at_css(LAST_NAME.to_s)
last = Nokogiri::XML::Node.new('last', people_doc)
last.content = lastname.content
person = Nokogiri::XML::Node.new('person', people_doc)
person << first
person << last
people << person
old_people = people_doc.css('people');
old_people.remove
people_doc << people
f = File.open('result.xml',"w"){|f| f.write(people_doc.to_xml)}
end
end
end
x = Challenge.new('source1.xml')
x.parse

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.