Blame view

ingester/scripts/map_metadata.pl 1.44 KB
9d00822b4   Paulo Gra├ža   Initial commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
  #!/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 "
  Usage: map_metadata.pl path/to/filename.xml [dc|degois]
  ";
      exit;
  }
  
  
  my $filename=$ARGV[0];
  unless (-e $filename) { 
      print STDERR "
  File: $filename doesn't exist!
  "; 
      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 "
  File: $schema-map.xslt doesn't exist!
  "; 
      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);