in reply to Quick check for XML DTD or Schema

Hi and welcome ron800, i'm not an expert of XML but when i need to get rid of such a shaggy thing i definetively use XML::Twig. It can do many many many things for you.

For example XML::Twig has an option (never seen before your question..) LoadDTD and is not to kill bugs on the twig.. ;=)

Is used to load DTD (internal OR external.. be aware!).
you can use also a DTDHandler to handle the DTD.
See the docs about XML::Twig methods (DTDHandler ) and also the dedicate section.

After reading the section you (me too..) discover that DTDHanlder receive two params: the twig itself and the DTD. So you can check for $_[1] ie the second element of the array passed to a subroutine.
cat with_dtd.xml <?xml version="1.0"?> <!DOCTYPE p [ <!ELEMENT p ANY> ]> <p>Hello world!</p> perl -MXML::Twig -e "$twig= new XML::Twig(LoadDTD=>1,DTDHandler=>sub{p +rint qq(FOUND\n) if $_[1]});$twig->parsefile($ARGV[0])" with_dtd.xml #output.. FOUND cat withOUT_dtd.xml <?xml version="1.0"?> <p>Hello world!</p> perl -MXML::Twig -e "my $twig= new XML::Twig(LoadDTD=>1,DTDHandler=>su +b{print qq(FOUND\n) if $_[1]});$twig->parsefile($ARGV[0])" withOUT_dt +d.xml #output..

HtH
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: Quick check for XML DTD or Schema.. XML::Twig solution
by ron800 (Novice) on Nov 06, 2014 at 20:25 UTC
    Thanks. I think I now can build the solution using XML::Twig
    my $t= XML::Twig->new(load_DTD => 1,DTDHandler=>\&checkit )->parsefile +( "$xmlfile"); sub checkit { my ($twig, $dtd) = @_; my $root=$twig->root; my @text=$root->att_names; if (grep /schemaLocation/, @text) { print "found schema\n"; } else { if ($twig->{twig_doctype}) { print "found dtd\n"; } else { print "no dtd/schema defined\n" } } }
      Bravo!
      L*
      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.