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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.