DIF - npolar/gcmd GitHub Wiki

Parsing DIF XML

The Gcmd::HashBuilder class enables you to generate hashes from a XML containing a single or multiple DIF metadata records.


  require "gcmd/hash_builder"
 
  builder = Gcmd::HashBuilder.new( dif_xml )
  result_array = builder.build_hash_documents
  

NOTE! Even though the defaults are set to support DIF out of the box the converter is actually quite generic and can be used to convert basically any xml document into Hashes. This will require some minor changes in the code. Mainly resetting some presets to your needs.

Writing DIF XML

The Gcmd::DifBuilder class can be used to generate a DIF (XML) document from a DIF Hash. So in order for this to work you need to make sure that the keys in your hash match the elements that occur in DIF. The result is a String containing the XML.


  require "gcmd/dif_builder"
  
  builder = Gcmd::DifBuilder.new( dif_hash )
  dif_xml = builder.build_dif

The code snippet above is dependant on an XML schema to work. The reason for this is a feature to make the XML schema valid and as complete as possible. By calling ruby #build_dif( dif_hash) the provided hash will first be synced with a template hash to make sure that the order of elements is in accordance with the schema. Any missing elements will be added as empty elements. By default the schema for the current version of DIF (v9.8.3) is loaded.

If you don't want syncing and completion to happen you can alternativly do the following:


  require "gcmd/dif_builder"
  
  builder = Gcmd::DifBuilder.new
  dif_xml = builder.build_xml( dif_hash )

This will generate an XML without any ordering or completion happening.

NOTE! The code for the conversions is fairly generic and could be easily adapted to convert other hash data into xml.

Validating DIF XML

The Gcmd::Schema class offers the posibility to validate DIF through the #validate_xml method. This works for both single DIF documents as for multiple documents in a container like OAI-PMH. The method returns an array of hashes containing validation information for each DIF.


  require "gcmd/schema"
  
  schema = Gcmd::Schema.new
  report = schema.validate_xml( xml_data )
  report.to_json

NOTE! In the example above the schema class is initialized with the default xml schema (DIF v9.8.3). In order to validate against a different version of DIF do the following schema = Gcmd::Schema.new( "schema.xsd" ).