Skip to content

Instantly share code, notes, and snippets.

@radar
Created December 11, 2010 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radar/737322 to your computer and use it in GitHub Desktop.
Save radar/737322 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'nokogiri'
Dir["**/*.xml"].each do |file|
p file
xml = File.read(file)
parsed_file = File.open(file, "w+")
chapter_id = File.basename(file, ".xml")
original_xml = Nokogiri::XML(xml)
nodes = original_xml.xpath("//*")
ids = nodes.map { |n| (n["id"] && n["id"].split("_").last.to_i) || 0 }
last_id = ids.max || 0
new_id = last_id + 1
nodes.each do |e|
next if !e["id"].nil?
e.set_attribute("id", "#{chapter_id}_#{new_id}")
new_id += 1
end
parsed_file.write(original_xml.to_xml)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment