Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

XML Validation

by Stegalex (Chaplain)
on Apr 02, 2002 at 20:31 UTC ( [id://156105]=perlquestion: print w/replies, xml ) Need Help??

Stegalex has asked for the wisdom of the Perl Monks concerning the following question:

I have looked high and low for a simple way to validate (not merely to check for well-formedness) an XML v 1.0 document against its dtd. I have looked through this site and am aware of merlyn's previous advice to use XML::Checker.

The problem is that the XML::Checker module is not well documented and the sample code throws the following exception:Can't call method "Start" on an undefined value at /usr/lib/perl5/site_perl/5.6.0/XML/Checker/Parser.pm line 194. Here's the code:
#!/usr/bin/perl -w use XML::Checker::Parser; my $parser = new XML::Checker::Parser (); die "Can't create object" unless (ref $parser); eval { local $XML::Checker::FAIL = \&my_fail; $parser->parsefile ("/usr/local/pd/ADS/index.xml"); }; if ($@) { # Either XML::Parser (expat) threw an exception or my_fail() died. #... your error handling code here ... print "you suck!\ngo eat a bone!\n"; } # Throws an exception (with die) when an error is encountered, this # will stop the parsing process. # Don't die if a warning or info message is encountered, just print a +message. sub my_fail { my $code = shift; die XML::Checker::error_string ($code, @_) if $code < 200; XML::Checker::print_error ($code, @_); }
and here's the DTD I am trying to validate against:
<?xml version="1.0" encoding="UTF-8"?> <!ELEMENT PDList (PD*)> <!ATTLIST PDList RunDateTime CDATA #REQUIRED> <!ELEMENT PD ( PayPlan, Grade, OccupationalSeries, NASAPositionTitle, OPMPositionTitle, FileName, NASALocation, DutyLocation, OrganizationalCode, NASAClassCode, SourceSystem)> <!ATTLIST PD PDNumber CDATA #REQUIRED> <!ELEMENT PayPlan (#PCDATA)> <!ELEMENT Grade (#PCDATA)> <!ELEMENT OccupationalSeries (#PCDATA)> <!ELEMENT NASAPositionTitle (#PCDATA)> <!ELEMENT OPMPositionTitle (#PCDATA)> <!ELEMENT FileName (#PCDATA)> <!ELEMENT NASALocation (#PCDATA)> <!ELEMENT DutyLocation (#PCDATA)> <!ELEMENT OrganizationalCode (#PCDATA)> <!ELEMENT NASAClassCode (#PCDATA)> <!ELEMENT SourceSystem (#PCDATA)>
and here's the xml that I want to validate:
<!DOCTYPE PDList SYSTEM "http://ifmp.nasa.gov/PDList.dtd"> <PDList RunDateTime="200203211000"> <PD PDNumber="10449"> <PayPlan>Text</PayPlan> <Grade>Text</Grade> <OccupationalSeries>Text</OccupationalSeries> <NASAPositionTitle>Text</NASAPositionTitle> <OPMPositionTitle>Text</OPMPositionTitle> <FileName>Text</FileName> <NASALocation>Text</NASALocation> <DutyLocation>Text</DutyLocation> <OrganizationalCode>Text</OrganizationalCode> <NASAClassCode>Text</NASAClassCode> <SourceSystem>Avue version whatever</SourceSystem> </PD> <PD PDNumber="1306x"> <PayPlan>Text</PayPlan> <Grade>Text</Grade> <OccupationalSeries>Text</OccupationalSeries> <NASAPositionTitle>Text</NASAPositionTitle> <OPMPositionTitle>Text</OPMPositionTitle> <FileName>Text</FileName> <NASALocation>Text</NASALocation> <DutyLocation>Text</DutyLocation> <OrganizationalCode>Text</OrganizationalCode> <NASAClassCode>Text</NASAClassCode> <SourceSystem>Avue version whatever</SourceSystem> </PD> <PD PDNumber="1307x"> <PayPlan>Text</PayPlan> <Grade>Text</Grade> <OccupationalSeries>Text</OccupationalSeries> <NASAPositionTitle>Text</NASAPositionTitle> <OPMPositionTitle>Text</OPMPositionTitle> <FileName>Text</FileName> <NASALocation>Text</NASALocation> <DutyLocation>Text</DutyLocation> <OrganizationalCode>Text</OrganizationalCode> <NASAClassCode>Text</NASAClassCode> <SourceSystem>Avue version whatever</SourceSystem> </PD> </PDList>
Any help or a really easy example script would be a big help. Thanks friends! I like chicken.

Replies are listed 'Best First'.
Re: XML Validation
by vek (Prior) on Apr 02, 2002 at 20:57 UTC
    Personally I wouldn't use XML::Checker. Why not try XML::LibXML instead:
    my $validator = XML::LibXML->new(); $validator->validation(1); #test if the XML is well formed: my $doc; eval { $doc = $validator->parse_string($theXML); }; if ($@) { # it's not well formed, your error handling code here... } #test if the XML conforms to the DTD: eval { $doc->validate(); }; if ($@) { # it doesn't conform, your error code handling here... }
      Thanks vek, works like a charm! I like chicken.
Re: XML Validation
by Matts (Deacon) on Apr 02, 2002 at 20:42 UTC
    libxml2 has much better validation than XML::Checker offers. Either use XML::LibXML or xmllint via a system call to do the validation.
      thanks for xmllint idea .. ++

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://156105]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (1)
As of 2024-04-19 18:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found