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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# $Id$
##
# Testcases for the XML Schema interface
#
use strict;
use warnings;
use lib './t/lib';
use TestHelpers qw(slurp);
use Test::More;
use XML::LibXML;
if ( XML::LibXML::LIBXML_VERSION >= 20510 ) {
plan tests => 12;
}
else {
plan skip_all => 'No Schema Support compiled.';
}
my $xmlparser = XML::LibXML->new();
my $file = "test/schema/schema.xsd";
my $badfile = "test/schema/badschema.xsd";
my $validfile = "test/schema/demo.xml";
my $invalidfile = "test/schema/invaliddemo.xml";
my $netfile = "test/schema/net.xsd";
# 1 parse schema from a file
{
my $rngschema = XML::LibXML::Schema->new( location => $file );
# TEST
ok ( $rngschema, 'Good XML::LibXML::Schema was initialised' );
eval { $rngschema = XML::LibXML::Schema->new( location => $badfile ); };
# TEST
ok( $@, 'Bad XML::LibXML::Schema throws an exception.' );
}
# 2 parse schema from a string
{
my $string = slurp($file);
my $rngschema = XML::LibXML::Schema->new( string => $string );
# TEST
ok ( $rngschema, 'RNG Schema initialized from string.' );
$string = slurp($badfile);
eval { $rngschema = XML::LibXML::Schema->new( string => $string ); };
# TEST
ok( $@, 'Bad string schema throws an excpetion.' );
}
# 3 validate a document
{
my $doc = $xmlparser->parse_file( $validfile );
my $rngschema = XML::LibXML::Schema->new( location => $file );
my $valid = 0;
eval { $valid = $rngschema->validate( $doc ); };
# TEST
is( $valid, 0, 'validate() returns 0 to indicate validity of valid file.' );
$doc = $xmlparser->parse_file( $invalidfile );
$valid = 0;
eval { $valid = $rngschema->validate( $doc ); };
# TEST
ok ( $@, 'Invalid file throws an excpetion.');
}
# 4 validate a node
{
my $doc = $xmlparser->load_xml(string => <<'EOF');
<shiporder orderid="889923">
<orderperson>John Smith</orderperson>
<shipto>
<name>Ola Nordmann</name>
</shipto>
</shiporder>
EOF
my $schema = XML::LibXML::Schema->new(string => <<'EOF');
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string"/>
<xs:element ref="shipto"/>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
EOF
my $nodelist = $doc->findnodes('/shiporder/shipto');
my $result = 1;
eval { $result = $schema->validate($nodelist->get_node(1)); };
# TEST
is( $@, '', 'validate() with element doesn\'t throw' );
# TEST
is( $result, 0, 'validate() with element returns 0' );
}
# 5 check that no_network => 1 works
{
my $schema = eval { XML::LibXML::Schema->new( location => $netfile, no_network => 1 ) };
# TEST
like( $@, qr{I/O error : Attempt to load network entity}, 'Schema from file location with external import and no_network => 1 throws an exception.' );
# TEST
ok( !defined $schema, 'Schema from file location with external import and no_network => 1 is not loaded.' );
}
{
my $schema = eval { XML::LibXML::Schema->new( string => <<'EOF', no_network => 1 ) };
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://example.com/namespace" schemaLocation="http://example.com/xml.xsd"/>
</xsd:schema>
EOF
# TEST
like( $@, qr{I/O error : Attempt to load network entity}, 'Schema from buffer with external import and no_network => 1 throws an exception.' );
# TEST
ok( !defined $schema, 'Schema from buffer with external import and no_network => 1 is not loaded.' );
}
|