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

This has come up about a jillion times before I think...

It took me a while to find this node: 31654. This one sent me off to grab XML::Checker::Parser: 31729.

XML::Checker::Parser, is probably what I want, but I've not yet gotten it to work. *sigh*

When I use the example from the man page for XML::Checker::Parser, I get this:

Can't call method "Start" on an undefined value at /usr/local/lib/perl5/site_perl/5.6.1/XML/Checker/Parser.pm line 194.

That line is:

$_checker->Start ($tag);

Now I figured,

eval { local $XML::Checker::FAIL = \&my_fail; $parser->parsefile ($ARGV[0]); };

might be wrong... Since, the only place where I could find a $_checker.*= was in sub parse, so I tried this:

eval { local $XML::Checker::FAIL = \&my_fail; $parser->parse; $parser->parsefile ($ARGV[0]); };

Which seemed stupid, but... no more runtime errors. The only problem is, it doesn't detect any errors in my doc either.

*sigh*, and this is just about the only XML validator for perl... So I'm at the end of my rope. Has anyone gotten XML::Checker::Parser to work? What am I don't wrong?

Replies are listed 'Best First'.
Re: XML Validator
by mirod (Canon) on Jul 31, 2001 at 07:43 UTC

    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.
Re: XML Validator
by damian1301 (Curate) on Jul 31, 2001 at 04:22 UTC
    Try removing the space between Start and ($tag); and make sure that $tag has something in it.

    I found it hard to really follow your question though. You might want to just include the whole block of code that is troublesome and you may get a better answer

    Best of luck

    $_.=($=+(6<<1));print(chr(my$a=$_));$^H=$_+$_;$_=$^H; print chr($_-39); # Easy but its ok.
      *LOL* That space won't do anything, the error relates to the fact that $_checker is undefined.

      Oh, and you're not supposed to modify the perl modules... ;)