map_metadata.pl 1.44 KB
#!/usr/bin/perl -w

use strict;                     # Good practice
use warnings;                   # Good practice
use Data::Dumper;               # Perl core module
use XML::LibXML;
use XML::LibXSLT;
use File::Spec;

#requires packages libxml-libxslt-perl, libxml-xpath-perl, libxml-perl

# (1) quit unless we have the correct number of command-line args
my $num_args = $#ARGV + 1;
if ($num_args < 1 and $num_args > 2) {
    print STDERR "\nUsage: map_metadata.pl path/to/filename.xml [dc|degois]\n";
    exit;
}


my $filename=$ARGV[0];
unless (-e $filename) { 
    print STDERR "\nFile: $filename doesn't exist!\n"; 
    exit;
}

my $schema = 'dc';  # default schema
if (defined($ARGV[1])) {
    $schema=$ARGV[1];
}

my @available_schemas = qw(dc degois);

# if schema is in available schemas array
if (! grep { $_ eq $schema} @available_schemas ) {
    die "$schema isn't a valid schema";
}


my ($volume, $directory, $file) = File::Spec->splitpath(__FILE__);

unless (-e $directory."../xslt/$schema-map.xslt") { 
    print STDERR "\nFile: $schema-map.xslt doesn't exist!\n"; 
    exit;    
}

XML::LibXSLT->max_depth(1000);

my $xslt = XML::LibXSLT->new();


my $source = XML::LibXML->load_xml(location => $filename);
my $style_doc = XML::LibXML->load_xml(location=>$directory."../xslt/$schema-map.xslt", no_cdata=>1);
my $stylesheet = $xslt->parse_stylesheet($style_doc);
my $results = $stylesheet->transform($source);

print $stylesheet->output_as_bytes($results);