Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I validate XML with XSD in Perl?

Tags:

xml

perl

xsd

This may be a simple question for most Perl programmers, I have only used Perl for two weeks so far and am very unfamiliar with the Perl packages.

I have a simple XSD file as below:

<?xml version="1.0" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
    <xsd:element name="elementname">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="field1" type="xsd:integer" minOccurs="0" maxOccurs="1"/>
                <xsd:element name="field2" type="xsd:string" minOccurs="0" maxOccurs="1"/>              
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

I would love to validate an XML file with the above XSD to ensure this is a valid XML. What Perl module should I use? I prefer a module that is available both on ActivePerl and Perl on *nix. Would be very helpful to post some code snippets.

Thanks

like image 982
John Avatar asked Jan 09 '10 22:01

John


2 Answers

I think you need XML::Validator::Schema from CPAN. Here's the README, and to install:

perl -MCPAN -e 'install XML::Validator::Schema'
like image 85
Brian Agnew Avatar answered Oct 16 '22 14:10

Brian Agnew


XML::LibXML::Schema has a validate method.

See also my answer to Why does my XSD file fail to parse with XML::LibXML?.

like image 38
Sinan Ünür Avatar answered Oct 16 '22 13:10

Sinan Ünür