-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_range_xml.pl
88 lines (65 loc) · 2.31 KB
/
parse_range_xml.pl
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
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
# Compile ISBN XML range file into isbniser range table
# Download the XML format ISBN Range file from:
# https://www.isbn-international.org/range_file_generation
# and process with this program. Then paste the resulting
# Perl array definition into isbniser.pl.
use strict;
use warnings;
use XML::Parser;
if ((scalar(@ARGV) < 1) || (!(-f $ARGV[0]))) {
print("Usage: perl parse_range_xml.pl RangeMessage.XML\n");
exit(0);
}
my @elstack;
my ($prefix, $agency, $range, $length, $messageDate);
my $parser = XML::Parser->new(
Handlers =>
{
Start => \&proc_start,
End => \&proc_end,
Char => \&proc_char
}
);
$prefix = "";
print(" \@ISBN_range = (\n");
$parser->parsefile($ARGV[0]);
print(" );\n");
print("\n");
print(" \$ISBN_database_date = \"$messageDate\";\n");
sub proc_start {
my ($expat, $element, %attrs) = @_;
my $line = $expat->current_line;
# print("Start $element on line $line\n");
push(@elstack, $element);
}
sub proc_end {
my ($expat, $element) = @_;
# print("End $element\n");
pop(@elstack);
}
sub proc_char {
my ($expat, $content) = @_;
if ($content !~ m/^\s*$/) {
# print("Content: ($content)\n");
my $tope = $elstack[$#elstack];
if ($tope eq "Prefix") {
if ($content =~ m/^\d\d\d\-\d/) {
$prefix = $content;
#print("Prefix ($prefix)\n");
} else {
$prefix = "";
}
} elsif ($tope eq "Agency") {
$agency = $content;
} elsif ($tope eq "Range") {
$range = $content;
} elsif ($tope eq "Length") {
$length = $content;
if ($prefix ne '') {
print(" [ \"$prefix\", \"$agency\", \"$range\", $length ],\n");
}
} elsif ($tope eq "MessageDate") {
$messageDate = $content;
}
}
}