yob / onix

A convenient mapping between ruby objects and the ONIX XML specification
MIT License
39 stars 33 forks source link

list extraction #2

Closed vivrana closed 14 years ago

vivrana commented 14 years ago

Hi, I noticed your format for list definition and created the following script to extract lists. The first step is to have the List definitions from the Onix CodeList xsd. Then change LIST_TO_RETRIEVE and the code list xsd file name appropriately. It's not too clean but I found that it helped me retrieve large lists quite easily (much better than doing it by hand). I hope it helps:

require 'xmlsimple'
xml = XmlSimple.xml_in('code_list.xsd')

LIST_TO_RETRIEVE = "List31"

xml.each do |key, value|

  if key.to_s == 'simpleType'

  value.each do |v|
    if v['name'].downcase == LIST_TO_RETRIEVE.downcase

      v['restriction'].each do |a|

        list_size = a['enumeration'].size
        a['enumeration'].each do |k|

          list_size -= 1
          val = ''
          #puts k['value'] + ' => "' + k['annotation'].class.to_s
          if k['value'][0..1] == '00' or k['value'].to_i != 0
            val = k['value'].to_i.to_s + ' => "' + k['annotation'][0]['documentation'][0] + '"'
          else
            val = '"' + k['value'] + '" => "' + k['annotation'][0]['documentation'][0] + '"'
          end

          val = val + ',' if list_size > 0
          puts val
        end

      end
    end
  end

  end

end

Thnx for Onix!

-Vivek.

yob commented 14 years ago

Thanks for prompting me on this, the new 0.8.4 gem includes generated files for all codelists. You can get them using:

ONIX::Lists.list(code_list_number)