-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathschema
93 lines (76 loc) · 2.53 KB
/
schema
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
89
90
91
92
93
#!/usr/bin/perl
use EPrints;
use strict;
my $repo = EPrints->new->current_repository;
exit( 0 ) unless( defined $repo );
my $xml = $repo->xml;
$repo->send_http_header( "content_type" => "application/xml; charset=utf-8" );
binmode(STDOUT, ":utf8");
print <<EOX;
<?xml version='1.0'?>
<xs:schema
targetNamespace="http://eprints.org/ep2/data/2.0"
xmlns="http://eprints.org/ep2/data/2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
>
EOX
my %elements;
my %types;
foreach my $datasetid ($repo->get_dataset_ids)
{
my $dataset = $repo->dataset( $datasetid );
next if $dataset->is_virtual;
foreach my $field ($dataset->fields)
{
# skip creators_name and other parts of compounds
next if $field->property("sub_name");
push @{$elements{$datasetid}}, $field->render_xml_schema( $repo );
my $type = $field->get_xml_schema_type();
if( $type !~ /^xs:/ )
{
$types{$type} ||= $field->render_xml_schema_type( $repo );
}
foreach my $sub_field (@{$field->{fields_cache}||[]})
{
my $type = $sub_field->get_xml_schema_type();
if( $type !~ /^xs:/ )
{
$types{$type} ||= $sub_field->render_xml_schema_type( $repo );
}
}
}
}
foreach my $datasetid (sort keys %elements)
{
# root element for this dataset
my $root = $xml->create_element( "xs:element", name => "${datasetid}s" );
my $complexType = $xml->create_element( "xs:complexType" );
$root->appendChild( $complexType );
my $choice = $xml->create_element( "xs:choice" );
$complexType->appendChild( $choice );
my $element = $xml->create_element( "xs:element", name => $datasetid, type => "dataset_$datasetid", minOccurs => "0", maxOccurs => "unbounded" );
$choice->appendChild( $element );
# dataset schema
$complexType = $xml->create_element( "xs:complexType", name => "dataset_$datasetid" );
# dataset fields
# TODO: this should be xs:all, but the DTD won't accept minOccurs=0
my $datasetAll = $xml->create_element( "xs:all", minOccurs => 0, maxOccurs => 1 );
$complexType->appendChild( $datasetAll );
foreach my $field_schemas (@{$elements{$datasetid}})
{
$datasetAll->appendChild( $field_schemas );
}
# dataset "id" attribute (attributes follow elements in schema)
my $id = $xml->create_element( "xs:attribute", name => "id", type => "xs:anyURI" );
$complexType->appendChild( $id );
print $repo->xml->to_string( $root, indent => 1 ) . "\n";
print $repo->xml->to_string( $complexType, indent => 1 ) . "\n";
}
foreach my $type (sort keys %types)
{
print $repo->xml->to_string( $types{$type}, indent => 1 ) . "\n";
}
print <<EOX;
</xs:schema>
EOX