require "nokogiri" module Ezid class MetadataTransformDatacite # Transforms the provided metadata hash into the appropriate format for datacite. Removes all "datacite.*" keys # and transforms these to the appropriate datacite xml. The resultant xml is then added to a single "datacite" key. def self.transform(hsh) # Render the datacite xml resource_opts = { "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", "xmlns" => "http://datacite.org/schema/kernel-4", "xsi:schemaLocation" => "http://datacite.org/schema/kernel-4 http://schema.datacite.org/meta/kernel-4/metadata.xsd" } xml_builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") { |builder| builder.resource(resource_opts) { builder.identifier(identifierType: hsh["datacite.identifiertype"] || "DOI") { builder.text hsh["datacite.identifier"] } builder.creators { builder.creator { builder.creatorName hsh["datacite.creator"] } } builder.titles { builder.title hsh["datacite.title"] } builder.publisher hsh["datacite.publisher"] builder.publicationYear hsh["datacite.publicationyear"] builder.resourceType(resourceTypeGeneral: hsh["datacite.resourcetypegeneral"]) { builder.text hsh["datacite.resourcetype"] } builder.descriptions { builder.description(descriptionType: "Abstract") { builder.text hsh["datacite.description"] } } } } # Using this save option to prevent NG from rendering new lines and tabs # between nodes. This to help with a cleaner anvl conversion. Similarly, # the sub should just remove the new line after the xml header that NG # adds, ex: # \n