I want to sort the file contents by country names :
require ‘nokogiri’
file_str = “C:\Users\TAX PAGE PROD\Production_validation.xml”
File.open(file_str) do |f|
doc = Nokogiri::XML(f) @node_set = doc.xpath("//field[@property=‘dummyProperty’]")
end
trgt_file_path = “C:\Users\Tax form
Demo\option_value_country_demo.txt”
File.open(trgt_file_path,“w”) do |f| @node_set.each do |node|
country = node.at_xpath("./arg0")[‘key’][/TAX ID for Country
(.*)/i,1]
option_value = node.xpath("./var[last()]/var-value").text
f.puts(“option value of #{country} is #{option_value}”)
end
end
The below code I don’t think elegant, although it worked.
File.open(trgt_file_path,“w”) do |f| @node_set.each do |node|
country = node.at_xpath("./arg0")[‘key’][/TAX ID for Country
(.*)/i,1]
option_value = node.xpath("./var[last()]/var-value").text
f.puts(“option value of #{country} is #{option_value}”)
end
end
Here’s a generic approach:
require ‘nokogiri’
dom = File.open(“x.xml”, “rb”){|io| Nokogiri.XML(io)}