Created
July 6, 2022 14:35
-
-
Save christianroy/f9ffada911a2bd946408f1bcd5b2a90f to your computer and use it in GitHub Desktop.
Short simple script to split a multi products ONIX file into many single product ONIX files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'nokogiri' | |
filename = './input.xml' | |
doc = Nokogiri::XML(File.open(filename)) | |
header = doc.css('Header') | |
products = doc.css('Product') | |
products.each do |product| | |
res = Nokogiri::XML::Document.new | |
onix_message = res.add_child(Nokogiri::XML::DocumentFragment.parse('<ONIXMessage xmlns="http://www.editeur.org/onix/3.0/reference" release="3.0">')) | |
onix_message.add_child(header) | |
onix_message.add_child(product) | |
output_filename = product.css('RecordReference').text + '.xml' | |
File.write(output_filename, res.to_xml) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment