Here is an example that should show you how to use XML::Checker::Parser.
Note that it also shows a bug in the module: characters within the doc element should trigger an error. See below for a patch for this.
#!/bin/perl -w use strict; use XML::Checker::Parser; $/=''; # records are now separated by an empty line # first parse, a valid document my $doc1=<DATA>; my $p= new XML::Checker::Parser; eval { local $XML::Checker::FAIL= \&my_fail; $p->parse( $doc1); }; if( $@) { print "parse 1 failed: $@\n"; } else { print "parse 1 OK\n"; } # second parse, an invalid document my $doc2=<DATA>; $p= new XML::Checker::Parser; eval { local $XML::Checker::FAIL= \&my_fail; $p->parse( $doc2); }; if( $@) { print "parse 2 failed: $@\n"; } else { print "parse 2 OK\n"; } # gets an error and dies after creating the error message sub my_fail { my ($code, $msg, %context)= @_; die " error $code ($msg) at line $context{line}, column $context{ +column}"; } __DATA__ <?xml version="1.0"?> <!DOCTYPE doc [ <!ELEMENT doc (elt+)> <!ELEMENT elt (#PCDATA)> ]> <!-- This document is valid --> <doc><elt>foo</elt><elt>bar</elt></doc> <?xml version="1.0"?> <!DOCTYPE doc [ <!ELEMENT doc (elt|elt2)+> <!ELEMENT elt (#PCDATA)> <!ELEMENT elt2 EMPTY> ]> <!-- This document is NOT valid --> <doc>toto<!-- should trigger error but does not --> <elt2>not empty <!-- error --></elt2><elt>foo</elt><elt>bar</elt></doc +>
Now here is the patch (which I will submit to the maintainer of the module):
228a229,232 > > # added by mirod > $checker->fail (149, "invalid text content [$str]") > unless $str=~ /^\s*$/; 1662c1666 < =head2 150 - 159 --- > =head2 149 - 159 1664a1669,1675 > > =item * > > B<149> - invalid text content [$value] > > A text was found in an element that should only include sub-elements > The text is not made of non-significant whitespace.
In reply to Re: XML Validator
by mirod
in thread XML Validator
by jettero
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |