Module: Serializers::MODS

Included in:
Document
Defined in:
lib/serializers/mods.rb

Overview

Convert a document to a MODS XML document

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) included(base)

Register this serializer in the Document list



8
9
10
11
12
13
14
# File 'lib/serializers/mods.rb', line 8

def self.included(base)
  base.register_serializer(
    :mods, 'MODS',
    ->(doc) { doc.to_mods.to_xml(indent: 2) },
    'http://www.loc.gov/standards/mods/'
  )
end

Instance Method Details

- (Nokogiri::XML::Document) to_mods(include_namespace = true)

Returns this document as a MODS XML document

By default, this will include the XML namespace declarations in the root mods element, making this document suitable to be saved standalone. Pass false to include_namespace to get a plain root element without namespaces, suitable for inclusion in a modsCollection.

Examples:

Write out this document as MODS XML

output = ''
doc.to_mods.write output


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/serializers/mods.rb', line 31

def to_mods(include_namespace = true)
  doc = Nokogiri::XML::Document.new
  mods = Nokogiri::XML::Node.new('mods', doc)
  doc.add_child(mods)

  if include_namespace
    mods.add_namespace_definition(nil, 'http://www.loc.gov/mods/v3')
    mods.add_namespace_definition('xlink', 'http://www.w3.org/1999/xlink')
    mods.add_namespace_definition('xsi', 'http://www.w3.org/2001/XMLSchema-instance')
    mods['xsi:schemaLocation'] = 'http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-0.xsd'
  end

  mods['version'] = '3.0'
  mods['ID'] = 'rletters_' + shasum

  unless title.blank?
    title_info = Nokogiri::XML::Node.new('titleInfo', doc)
    mods.add_child(title_info)

    title_elt = Nokogiri::XML::Node.new('title', doc)
    title_info.add_child(title_elt)

    title_elt.content = title
  end

  unless formatted_author_list.nil? || formatted_author_list.count == 0
    formatted_author_list.each do |a|
      name = Nokogiri::XML::Node.new('name', doc)
      mods.add_child(name)

      name['type'] = 'personal'

      first_name_elt = Nokogiri::XML::Node.new('namePart', doc)
      name.add_child(first_name_elt)
      first_name_elt.content = a.first
      first_name_elt['type'] = 'given'

      last_name = ''
      last_name << " #{a.von}" unless a.von.blank?
      last_name << a.last
      last_name << ", #{a.suffix}" unless a.suffix.blank?
      last_name_elt = Nokogiri::XML::Node.new('namePart', doc)
      name.add_child(last_name_elt)
      last_name_elt.content = last_name
      last_name_elt['type'] = 'family'

      role = Nokogiri::XML::Node.new('role', doc)
      name.add_child(role)
      role_term = Nokogiri::XML::Node.new('roleTerm', doc)
      role.add_child(role_term)
      role_term.content = 'author'
      role_term['type'] = 'text'
      role_term['authority'] = 'marcrelator'
    end
  end

  type_of_resource = Nokogiri::XML::Node.new('typeOfResource', doc)
  mods.add_child(type_of_resource)
  type_of_resource.content = 'text'

  article_genre = Nokogiri::XML::Node.new('genre', doc)
  mods.add_child(article_genre)
  article_genre.content = 'article'

  article_origin_info = Nokogiri::XML::Node.new('originInfo', doc)
  mods.add_child(article_origin_info)
  article_issuance = Nokogiri::XML::Node.new('issuance', doc)
  article_origin_info.add_child(article_issuance)
  article_issuance.content = 'monographic'
  unless year.blank?
    date_issued = Nokogiri::XML::Node.new('dateIssued', doc)
    article_origin_info.add_child(date_issued)
    date_issued.content = year
  end

  related_item = Nokogiri::XML::Node.new('relatedItem', doc)
  mods.add_child(related_item)
  related_item['type'] = 'host'

  unless journal.blank?
    title_info = Nokogiri::XML::Node.new('titleInfo', doc)
    related_item.add_child(title_info)
    title_info['type'] = 'abbreviated'

    title_elt = Nokogiri::XML::Node.new('title', doc)
    title_info.add_child(title_elt)
    title_elt.content = journal
  end

  journal_origin_info = Nokogiri::XML::Node.new('originInfo', doc)
  related_item.add_child(journal_origin_info)
  journal_issuance = Nokogiri::XML::Node.new('issuance', doc)
  journal_origin_info.add_child(journal_issuance)
  journal_issuance.content = 'continuing'
  unless year.blank?
    date_issued = Nokogiri::XML::Node.new('dateIssued', doc)
    journal_origin_info.add_child(date_issued)
    date_issued.content = year
  end

  journal_genre_1 = Nokogiri::XML::Node.new('genre', doc)
  related_item.add_child(journal_genre_1)
  journal_genre_1.content = 'periodical'
  journal_genre_1['authority'] = 'marcgt'
  journal_genre_2 = Nokogiri::XML::Node.new('genre', doc)
  related_item.add_child(journal_genre_2)
  journal_genre_2.content = 'academic journal'

  part = Nokogiri::XML::Node.new('part', doc)
  related_item.add_child(part)
  unless volume.blank?
    detail = Nokogiri::XML::Node.new('detail', doc)
    part.add_child(detail)
    detail['type'] = 'volume'
    number_elt = Nokogiri::XML::Node.new('number', doc)
    detail.add_child(number_elt)
    number_elt.content = volume
  end

  unless number.blank?
    detail = Nokogiri::XML::Node.new('detail', doc)
    part.add_child(detail)
    detail['type'] = 'issue'
    number_elt = Nokogiri::XML::Node.new('number', doc)
    detail.add_child(number_elt)
    number_elt.content = number
    caption = Nokogiri::XML::Node.new('caption', doc)
    detail.add_child(caption)
    caption.content = 'no.'
  end

  unless pages.blank?
    extent = Nokogiri::XML::Node.new('extent', doc)
    part.add_child(extent)
    extent['unit'] = 'page'
    unless start_page.blank?
      start_elt = Nokogiri::XML::Node.new('start', doc)
      extent.add_child(start_elt)
      start_elt.content = start_page
    end
    unless end_page.blank?
      end_elt = Nokogiri::XML::Node.new('end', doc)
      extent.add_child(end_elt)
      end_elt.content = end_page
    end
  end

  unless year.blank?
    date = Nokogiri::XML::Node.new('date', doc)
    part.add_child(date)
    date.content = year
  end

  unless doi.blank?
    identifier = Nokogiri::XML::Node.new('identifier', doc)
    mods.add_child(identifier)
    identifier['type'] = 'doi'
    identifier.content = doi
  end

  doc
end