How can I validate XML with XSD in Perl?
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:sequ开发者_如何学Goence>
</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
I think you need XML::Validator::Schema from CPAN. Here's the README, and to install:
perl -MCPAN -e 'install XML::Validator::Schema'
XML::LibXML::Schema has a validate
method.
See also my answer to Why does my XSD file fail to parse with XML::LibXML?.
精彩评论