koha-to-dspace/marc2dc.pl

94 lines
2.9 KiB
Perl
Executable File

#!/usr/bin/perl -w
use MARC::Crosswalk::DublinCore;
use MARC::File::USMARC;
use utf8;
binmode(STDOUT, ":utf8");
$/ = chr(29); # MARC record separator
print qq|<collection>\n|;
while (my $blob = <>) { # suck in one MARC record at a time
# convert the MARC to DC
my $marc = MARC::Record->new_from_usmarc( $blob );
my $crosswalk = MARC::Crosswalk::DublinCore->new( qualified => 0 );
my $dc = $crosswalk->as_dublincore( $marc );
my $has_content = 0;
# output the DC as XML
for( $dc->elements ) {
if (!$has_content) {
$has_content = 1;
print qq|<dublin_core>\n|;
}
my $element = lc($_->name);
my $qualifier = lc($_->qualifier);
my $scheme = lc($_->scheme);
my $content = $_->content;
# escape reserved characters
$content =~ s/&/&amp;/gs;
$content =~ s/</&lt;/gs;
$content =~ s/>/&gt;/gs;
# munge attributes for DSpace compatibility
if ($element eq 'creator') {
$element = 'contributor';
$qualifier = 'author';
$content =~ s/[0-9]//g;
$content =~ s/^\s+|\s+$//g;
}
if ($element eq 'format') {
$element = 'description';
$qualifier = '';
}
if ($element eq 'language') {
$qualifier = 'iso';
}
if ($element eq 'type') {
$element = 'type';
$qualifier = '';
}
if ($element eq 'subject') {
$content =~ s/[0-9]//g;
$content =~ s/^\s+|\s+$//g;
}
if ($element eq 'title') {
$content =~ s/\[[^][]*\]//g;
$content =~ s/^\s+|\s+$//g;
}
if ($element eq 'identifier') {
$element = 'relation';
$qualifier = 'uri';
}
printf qq| <dcvalue element="%s"|, $element;
printf qq| qualifier="%s"|, $qualifier if $qualifier;
printf qq| qualifier="%s"|, $scheme if $scheme and !$qualifier;
if ($element eq 'title' || $element eq 'description') {
#if ($element eq 'title' || $element eq 'description' || $element eq 'subject') {
printf qq| language="es_AR">%s</dcvalue>\n|, $content;
} else {
printf qq|>%s</dcvalue>\n|, $content;
}
}
# if ($ARGV[0]) {
# printf qq| <dcvalue element="dspace"|, $element;
# printf qq| qualifier="entity"|, $qualifier if $qualifier;
# printf qq| qualifier="%s"|, $scheme if $scheme and !$qualifier;
# printf qq|>%s</dcvalue>\n|, $content;
# }
print qq|</dublin_core>\n| if $has_content;
}
print qq|</collection>\n|;
exit;