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.