8

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

brian d foy
  • 129,424
  • 31
  • 207
  • 592
John
  • 221
  • 1
  • 5
  • 14
  • Duplicate: "Is there a Perl module that validates an XML against a schema?" - http://stackoverflow.com/questions/322080/is-there-a-perl-module-that-validates-an-xml-against-a-schema – draegtun Jan 10 '10 at 11:09

2 Answers2

5

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

perl -MCPAN -e 'install XML::Validator::Schema'
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • @Brian - thanks for your quick response, I am not able to find this module on ActivePerl. Any good alternative on ActivePerl? I am hoping to find a module that is available and runs on both Windows and Linux. – John Jan 09 '10 at 22:50
  • If you have to use perl on windows I suggest you use strawberry perl instead. ** http://strawberryperl.com/ ** It's much better than ActivePerl (when it comes to package coverage (and other things too)) I supplies a proper CPAN chell to install and compile a lot more packages. – Nifle Jan 09 '10 at 23:06
  • I don't know, I'm afraid. I would hope ActivePerl would support CPAN, but perhaps not. – Brian Agnew Jan 09 '10 at 23:06
  • @Nifle - Unfortunately, I don't have a choice to switch over to strawberryperl. Thanks for bringing it up anyway. – John Jan 09 '10 at 23:09
  • 1
    ActivePerl does "support CPAN", and this module (and thousands of others). It's in the list at http://ppm4.activestate.com/idx/X...Z.html – Ether Jan 09 '10 at 23:13
  • Too bad. I was in heaven (slight exaggeration) wen we made the switch. It made life soo much easier (we are talking a few man-weeks not having to replicate/recode CPAN packages) – Nifle Jan 09 '10 at 23:14
  • @Ether - Does ActivePerl really support a CPAN shell with compilations of modules now? If so it's good news, I haven't been paying attention since we switched (about two years ago). – Nifle Jan 09 '10 at 23:20
  • @Ether - I am undoubtly a perl newbie, so I did see this module in the list but don't see in package manager. How can I install this module without using package manager or have package manager to download/install for me? – John Jan 09 '10 at 23:21
  • @Ether - I'd love to have a proper discussion with you about activestate and perl. I used it for many years before the switch to the berry, but alas, this is not the place. – Nifle Jan 09 '10 at 23:22
  • @Nifle - I would love to hear your personal thoughts about strawberryper, what are the pros and cons VS ActivePerl? Perhaps opening a community wiki will be more appropriate. – John Jan 09 '10 at 23:23
  • @John: what's the problem with using the package installer? Issues with ppm might be better spun off into its own question. – Ether Jan 09 '10 at 23:26
  • @Ether - No problem with package manager, I just don't see this module while I can find it in the list. Not sure how to install it in this case. Any pointer? – John Jan 09 '10 at 23:27
  • See http://stackoverflow.com/questions/71513/which-version-of-perl-should-i-use-on-windows for a comparison of the two perls. – Ether Jan 09 '10 at 23:27
  • @Ether *very short answer* - It was very often needed to add repository's to the package manager for perl packages. And finding them was sometimes quite a bit of work (and sometimes not found). With strawberry all that went away, you used CPAN and it did *make, make test, make install* and you where done. ** caveat ** *All experiences are more than two years old* – Nifle Jan 09 '10 at 23:35
  • @Ether - We where **very** pleased with activestate's perl for many years. – Nifle Jan 09 '10 at 23:37
  • The module brian suggested is pure Perl, so it should build easily, AS Perl should be no problem. I suggest managing ALL your module installs through PPM. Install PPM::Make through PPM GUI, then build a PPM and install it from a DOS window. BTW, Kobesearch has links to PPMs for modules--it can be a big help: http://cpan.uwinnipeg.ca/htdocs/faqs/cpan-search.html – daotoad Jan 10 '10 at 05:45
  • `ppm` comes with the most popular repositories pre-configured so you can add them with a couple of clicks without having to hunt for URLs. – Sinan Ünür Jan 10 '10 at 09:14
  • @Sinan - Nope, ppm doesn't ship with this particular module by default, correct me if I am wrong. I managed to install it via cpan (part of ActivePerl). So it is confirmed that ActivePerl has a cpan shell now. – John Jan 12 '10 at 00:34
2

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

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

Community
  • 1
  • 1
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339