Skip to content

Instantly share code, notes, and snippets.

@rwd
Created August 13, 2014 10:20
Show Gist options
  • Save rwd/bbf75b4b21228bfa375b to your computer and use it in GitHub Desktop.
Save rwd/bbf75b4b21228bfa375b to your computer and use it in GitHub Desktop.
Merge multiple rdf:Description elements with the same rdf:about attribute
#!/usr/bin/env ruby
#
# @file
# This script takes an RDF file containing multiple rdf:Description elements
# with the same rdf:about attribute, combines them into a single
# rdf:Description for each rdf:about, and outputs the resultant RDF.
#
require 'nokogiri'
unless ARGV[0]
puts "Usage: #{__FILE__} RDF_FILENAME"
exit
end
RDF_FILENAME=ARGV[0]
f = File.open(RDF_FILENAME)
rdf = Nokogiri::XML(f) do |config|
config.default_xml.noblanks
end
f.close
rdf_descriptions = {}
rdf.root.xpath('./rdf:Description').each do |rdf_description|
rdf_about = rdf_description['rdf:about']
if rdf_descriptions.has_key?(rdf_about)
rdf_description.children.each do |child|
rdf_descriptions[rdf_about] << child
end
else
rdf_descriptions[rdf_about] = rdf_description
end
rdf_description.remove
end
rdf_descriptions.each_value do |rdf_description|
rdf.root << rdf_description
end
puts rdf.to_xml(:indent => 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment