Is there a Perl module that validates an XML against a schema?

I need to validate an XML agaist a schema.

I tried XML::SAX::ParserFactory; XML::Validator::Schema and related modules but looks like they are limited. Limited in the sense that it didn't recognize schema elements such as xsd:unique, xsd:group, xsd:keyref, xsd:union and xsd:key.

Are these xsd:unique, etc. new additions?

Appreciate if you can point me to some Perl modules which are upto date and upto the task.

Thanks in advance.

Answers


Have you tried validating it with xmllint?


Have a look at XML::LibXML

This uses the highly regarded libxml2 library which most people cite as a successor to Expat and XML::Parser based modules.

For schema validation look at XML::LibXML::Schema

use XML::LibXML;
use XML::LibXML::Schema;

my $schema = XML::LibXML::Schema->new(location => 'file.xsd' );
my $parser = XML::LibXML->new;

my $xml    = 'file.xml';
my $doc    = $parser->parse_file( $xml );

eval { $schema->validate( $doc ) };
die $@ if $@;

say "$xml is valid\n";

NB. I still mainly a XML::Twig user and barely touch XML schema so I don't know if XML::LibXML::Schema will solve the problem you have but it possibly is the best solution on CPAN for your needs.


@Thushan Is it absolutely necessary for you to use Perl? If not, why not give a try to XMLSTARLET? It's a quite nice toolkit for manipulating XMLs in a terminal!

It supports not just legacy DTDs and the XSD as you need, also it supports Relax-NG schemas. Check it's documentation to learn how to Validate XML Documents with xmlstarlet I hope this helps you ;)

Have a good day!

PD Oh I see you used xmllint! It's great too, but I prefer xmlstarlet =) Give it a try ;)


Need Your Help

Python optimisations in this code?

python optimization

I have two fairly simple code snippets and I'm running both of them a very large amount of times; I'm trying to determine if there's any optimisation I can do to speed up the execution time. If the...

NSDate Showing Wrong Day

ios nsstring nsdate nsdateformatter

I am taking an NSDate, and pulling just a 2-digit number, representing the day of the month into an NSString. One of the dates in question is: