Subject: | XML::LibXML::Schema will stop validation on first error |
I'm trying to use the XML::LibXML::Schema to validate a xml file against
a xsd. The problem is if my xml has multiple semantic issues the
validation will die on the first one and not report the others.
# Find the first error, no reference to <foobar/>
bash-3.2$ ./test.pl test.xsd test.xml
xmlfile <test.xml> failed validation: test.xml:14: Schemas validity
error : Element 'foobar': This element is not expected.
# Removing the first error it finds another "ten" is not an unsigned int
xmlfile <test.xml> failed validation: test.xml:11: Schemas validity
error : Element 'allocation': 'ten' is not a valid value of the atomic
type 'xs:unsignedInt'.
# Changing "ten" to 10 fixes this issue
bash-3.2$ ./test.pl test.xsd test.xml
No issues found
Multiple errors can be found fine with xmllint:
$ xmllint --schema test.xsd test.xml
<?xml version="1.0" encoding="UTF-8"?>
<request xmlns:xsd="http://www.w3.org/2001/XMLSchema" type="test">
<channel name="channel">
<username>user</username>
<password>pass</password>
</channel>
<hotel id="ten">
<date from="2009-07-07" to="2009-07-17"/>
<room id="1">
<allocation>10</allocation>
</room>
</hotel>
<foobar/>
</request>
test.xml:8: element hotel: Schemas validity error : Element 'hotel',
attribute 'id': 'ten' is not a valid value of
the atomic type 'xs:unsignedInt'.
test.xml:14: element foobar: Schemas validity error : Element 'foobar':
This element is not expected.
test.xml fails to validate
I've attached the xsd, xml and perl script I used.
Thanks,
Paulo
Subject: | test.xml |
<?xml version="1.0" encoding="UTF-8"?>
<request type="test" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<channel name="channel">
<username>user</username>
<password>pass</password>
</channel>
<hotel id="ten">
<date from="2009-07-07" to="2009-07-17"/>
<room id="1">
<allocation>10</allocation>
</room>
</hotel>
<foobar/>
</request>
Subject: | test.xsd |
Message body not shown because it is not plain text.
Subject: | test.pl |
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
#use Carp;
use vars qw( $xsd $xml $schema $parser $doc );
use XML::LibXML;
#
# MAIN
#
my $xsd = $ARGV[0];
my $xml = $ARGV[1];
$schema = XML::LibXML::Schema->new(location => $xsd);
$parser = XML::LibXML->new( recover => 2 );
$doc = $parser->parse_file($xml);
eval { $schema->validate($doc) };
if ( $@ ) {
warn "xmlfile <$xml> failed validation: $@" if $@; exit(1);
}
else { print "No issues found\n"; }