5 mods2marc.pl - convert MODS XML back to MARC (ISO2709)
9 mods2marc.pl export.marc mods.xml [mods2.xml ... ]
13 This script will convert MODS format
14 L<http://www.loc.gov/standards/mods/>
15 back to MARC (ISO2709) format.
17 Since conversion back to MARC is not simple, lot of things are hard-coded
20 This script B<is somewhat specific> to MODS export from
21 Faculty of Electrical Engineering and Computing
22 so you might want to edit it (among other thing, it includes a lot
23 of fields which are in Croatian).
25 Feel free to hack this script and convert it to your own needs.
29 This script will parse imput XML twice: once with C<XML::Twig> and
30 then each entry with C<XML::Simple> to produce in-memory structure.
31 That's because I wanted to keep node selection logical (and perl-like).
33 If you don't like it, you can rewrite this script to use XPATH. I tried
34 and failed (it seems that MODS is too complicated for my limited knowledge
47 my $marc_file = shift @ARGV || die "$0: need MARC export file";
48 die "$0: need at least one MODS XML file" if (! @ARGV);
55 my $ENCODING = 'ISO-8859-2';
57 my $twig=XML::Twig->new(
58 twig_roots => { 'mods' => \&mods },
59 output_encoding => 'UTF8',
62 my $utf2iso = Text::Iconv->new("UTF8", $ENCODING);
64 foreach my $xml_file (@ARGV) {
66 $twig->parsefile($xml_file);
71 print "Saving MARC file...\n";
73 $marc->output({file=>"> $marc_file",'format'=>"usmarc"});
78 my $xml=$elt->xml_string;
79 my $ref = XMLin('<xml>'.$xml.'</xml>',
89 'identifier' => 'type',
94 'place' => 'placeTerm',
95 'physicalDescription' => 'extent',
96 'roleTerm' => 'content',
98 ContentKey => '-content',
104 my $m_cache = \shift || die "need m_cache";
105 my $fld = shift || die "need field!";
107 my $data = shift || return;
109 #print "add: $fld",($sf ? "^".$sf : ''),": $data\n";
112 push @{$$m_cache->{tmp}->{$fld}}, $sf;
114 push @{$$m_cache->{tmp}->{$fld}}, $utf2iso->convert($data);
118 my $m_cache = \shift || die "need m_cache";
119 foreach my $fld (@_) {
120 #print "marc_rep: $fld\n";
121 push @{$$m_cache->{array}->{$fld}}, [ @{$$m_cache->{tmp}->{$fld}} ] if ($$m_cache->{tmp}->{$fld});
122 delete $$m_cache->{tmp}->{$fld};
127 my $m_cache = \shift || die "need m_cache";
128 foreach my $fld (@_) {
129 #print "marc_single: $fld\n";
131 die "$fld already defined! not single?" if ($$m_cache->{single}->{$fld});
133 $$m_cache->{single}->{$fld} = \@{$$m_cache->{tmp}->{$fld}} if ($$m_cache->{tmp}->{$fld});
134 delete $$m_cache->{tmp}->{$fld};
139 my $m_cache = \shift || die "need m_cache";
140 my $fld = shift || die "need field!";
142 my $data = shift || return;
144 marc_add($$m_cache,$fld,$sf,$data);
145 marc_rep($$m_cache,$fld);
148 sub marc_add_single {
149 my $m_cache = \shift || die "need m_cache";
150 my $fld = shift || die "need field!";
152 my $data = shift || return;
154 marc_add($$m_cache,$fld,$sf,$data);
155 marc_single($$m_cache,$fld);
159 # Journals start with c- in our MODS
160 $journal = 1 if ($ref->{recordInfo}->{recordIdentifier} =~ m/^c-/);
162 foreach my $t (@{$ref->{subject}->{topic}}) {
163 marc_add($m_cache,'610','a', $t);
164 marc_rep($m_cache,'610');
170 foreach my $name (@{$ref->{name}}) {
171 my $role = $name->{role}->{roleTerm}->{content};
173 if ($role eq "author") {
174 marc_add($m_cache,$fld_700,'a',$name->{namePart}->{family});
175 marc_add($m_cache,$fld_700,'b',$name->{namePart}->{given});
176 marc_add($m_cache,$fld_700,'4',$role);
178 marc_rep($m_cache,$fld_700);
180 # first author goes in 700, others in 701
182 } elsif ($role eq "editor" or $role eq "illustrator") {
183 marc_add($m_cache,'702','a',$name->{namePart}->{family});
184 marc_add($m_cache,'702','b',$name->{namePart}->{given});
185 marc_add($m_cache,'702','4',$role);
186 marc_rep($m_cache,'702');
187 } elsif ($role eq "corporate") {
188 marc_add_single($m_cache,"$fld_710\t0 ",'a',$name->{namePart});
190 } elsif ($role eq "conference") {
191 marc_add_single($m_cache,"$fld_710\t1 ",'a',$name->{namePart});
194 die "FATAL: don't know how to map role '$role'" if ($role);
198 my $note = $ref->{note};
201 foreach my $n (split(/\s*;\s+/, $note)) {
202 if ($n =~ s/bibliogr:\s+//i) {
203 marc_add_rep($m_cache,'320','a',"Bibliografija: $n");
204 } elsif ($n =~ s/ilustr:\s+//i) {
205 marc_add($m_cache,'215','c', $n);
207 marc_add_rep($m_cache,'320','a',$n);
213 my $type = $ref->{identifier}->{type};
216 if ($type eq "isbn") {
217 marc_add_rep($m_cache,'010','a',$ref->{identifier}->{content});
218 } elsif ($type eq "issn") {
219 marc_add_rep($m_cache,'011','a',$ref->{identifier}->{content});
220 } elsif ($type eq "uri") {
221 marc_add_rep($m_cache,'856','u',$ref->{identifier}->{content});
223 die "unknown identifier type $type";
227 my $phy_desc = $ref->{physicalDescription};
230 foreach my $t (split(/\s*;\s+/, $phy_desc)) {
231 if ($t =~ m/([^:]+):\s+(.+)$/) {
234 die "can't parse $t";
237 my $data = $tmp->{pagin};
238 $data .= ", " if ($data);
240 $data .= $tmp->{str}." str";
242 marc_add($m_cache,'215','a', $data) if ($data);
243 marc_add($m_cache,'215','d', $tmp->{visina});
245 marc_rep($m_cache,'215');
247 marc_add_single($m_cache,'001',undef,$ref->{recordInfo}->{recordIdentifier});
249 marc_add($m_cache,'200','a',$ref->{titleInfo}->{title});
250 marc_add($m_cache,'200','e',$ref->{titleInfo}->{subTitle});
251 marc_single($m_cache,'200');
253 foreach my $c (@{$ref->{classification}}) {
254 if ($c->{'authority'} eq "udc") {
255 marc_add_rep($m_cache,'675','a', $c->{'content'});
259 foreach my $ri (@{$ref->{relatedItem}}) {
260 my $related = $ri->{type};
262 if ($related eq "series") {
263 marc_add_rep($m_cache,'225','a',$ri->{titleInfo}->{title});
264 foreach my $pn (@{$ri->{titleInfo}->{partNumber}}) {
265 marc_add_rep($m_cache,'999','a',$pn);
267 } elsif ($related eq "preceding") {
268 marc_add_rep($m_cache,'430','a',$ri->{titleInfo}->{title});
270 die "can't parse related item type $related" if ($related);
275 marc_add_single($m_cache,'205','a',$ref->{originInfo}->{edition});
277 my $publisher = $ref->{originInfo}->{publisher};
278 if ($publisher =~ m,^(.+?)\s*/\s*(.+)$,) {
279 marc_add($m_cache,'210','a', $2);
280 marc_add($m_cache,'210','c', $1);
282 marc_add($m_cache,'210','c', $publisher);
285 marc_add($m_cache,'210','a',$ref->{originInfo}->{place});
286 marc_add($m_cache,'210','d',$ref->{originInfo}->{dateIssued});
288 marc_single($m_cache,'210');
290 marc_add_single($m_cache,'326','a',$ref->{originInfo}->{frequency}) if ($journal);
293 print "$nr " if ($nr % 100 == 0);
297 $bib_level = "s" if ($journal);
298 my $m=$marc->createrecord({leader=>"00000na".$bib_level." 2200000 a 4500"});
300 foreach my $fld (keys %{$m_cache->{array}}) {
301 foreach my $arr (@{$m_cache->{array}->{$fld}}) {
302 #print "array = ",Dumper($arr);
304 # do we have indicators?
305 if ($fld =~ m/^(.+)\t(.)(.)$/) {
309 $marc->addfield({record=>$m,
318 foreach my $fld (keys %{$m_cache->{single}}) {
319 #print "single = ",Dumper($m_cache->{single}->{$fld});
321 # do we have indicators?
322 if ($fld =~ m/^(.+)\t(.)(.)$/) {
326 $marc->addfield({record=>$m,
330 value=>$m_cache->{single}->{$fld}
336 $t->purge; # frees the memory