How can I validate XML with XSD in Perl?

This may be a simple question for most Perl programmers; I have used Perl for only two weeks and am very new to Perl packages.

I have a simple XSD file as shown 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 like to check the XML file with the above XSD to make sure that it is valid XML. Which Perl module should I use? I prefer the module available on both ActivePerl and Perl on * nix. It would be very helpful to post some code snippets.

thanks

+7
xml perl xsd
source share
2 answers

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

 perl -MCPAN -e 'install XML::Validator::Schema' 
+4
source share

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

See also my answer to Why my XSD file cannot parse XML :: LibXML? .

+2
source share

All Articles