How can I validate XML with XSD in Perl?

好久不见. 提交于 2019-12-22 04:55:36

问题


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


回答1:


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

perl -MCPAN -e 'install XML::Validator::Schema'



回答2:


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

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



来源:https://stackoverflow.com/questions/2035232/how-can-i-validate-xml-with-xsd-in-perl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!