in reply to Quick check for XML DTD or Schema

Well, my gut reaction would be to open the file and read in the first few lines and see if any of them match a regex. But that assumes that what you are looking for would always be near the start of the file.

On a more general note, anytime I hear a question that begins with "Is it possible to do ..." my immediate response is always "Yes! Just give me enough time and money." If I can't do it myself, I can hire the people who can. Do you want faster than light travel? Do you want anti-gravity? Just give me enough time and money.

Update: Here is a very crude approach that might work. (Please note that this is basic logic, NOT working code!)

open file for reading; $found_dtd = 0; while not end of file { next unless string has pattern known to be after DTD or schema if line matches DTD or schema regex { $found_dtd = 1; last; } } return $found_dtd;

You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.

Replies are listed 'Best First'.
Re^2: Quick check for XML DTD or Schema
by ron800 (Novice) on Nov 06, 2014 at 12:10 UTC
    Makes sense. Assuming the declaration is at the top, is it sufficient in the case of a DTD, to check for "<!DOCTYPE root-element'? Or for a schema, to check that the path of the XML schema is in the schemaLocation attribute of the XML file root element, i.e. check for 'xsi:schemaLocation' or 'xsi:noschemalocation'?

      I would think so. Try it. :)

      Using one of the modules named below would work, too, but I got the sense from the OP that you don't want to do that, just a real quick peek at the file without the overhead of doing any work with it.

      You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.