tags:

views:

448

answers:

2

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  A: 

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

perl -MCPAN -e 'install XML::Validator::Schema'
Brian Agnew
@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
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
I don't know, I'm afraid. I would hope ActivePerl would support CPAN, but perhaps not.
Brian Agnew
@Nifle - Unfortunately, I don't have a choice to switch over to strawberryperl. Thanks for bringing it up anyway.
John
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
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
@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
@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
@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
@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
@John: what's the problem with using the package installer? Issues with ppm might be better spun off into its own question.
Ether
@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
See http://stackoverflow.com/questions/71513/which-version-of-perl-should-i-use-on-windows for a comparison of the two perls.
Ether
@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
@Ether - We where **very** pleased with activestate's perl for many years.
Nifle
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
`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
@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
+1  A: 

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

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

Sinan Ünür