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

Hello! I am trying to figure out an easy way to see if user-submitted XHTML is syntactically-valid. This is proving to be a bit of a challenge. Currently, I am trying something like this:
use XML::LibXML; my $xml_string = "<test>Teststring</test>"; my $parser = XML::LibXML->new; $parser->validation(1); $parser->parse_string($xml_string);
This fails with "validity error : Validation failed: no DTD found !". Fair enough, there's no DTD. But I just want to check to see that tags are balanced and properly-constructed for some arbitrary segment of code. I tried using the SkipDTD flag, but it had no effect. Is there an easy way to validate small snippets of code in this way?

Replies are listed 'Best First'.
Re: Validating XHTML
by gube (Parson) on Apr 28, 2006 at 13:30 UTC

    Hi try this,

    If you need to check whether the xml content is correct or not you can use this below code. If you need to parse the file just call the method parse_file method. If there is error found in xml file it print and check in $@, you can see the error

    use XML::LibXML; my $xml_string = "<test>Teststring</test>"; my $parser = XML::LibXML->new; eval { $parser->parse_string($xml_string) }; print "it's not a valid xml" if ($@) ;
      Hey! That works really well! I'm a little confused that taking the validation() line out made it work better, but the eval trick is pretty nice.

        XML (and, hence, XHTML) has two different levels of "correctness". First you can check that the document is well-formed. That means that it conforms to the syntax rules for an XML document (balanced tags, correctly formed attributes, that kind of thing). Then you can check that it is valid. This means checking it against a DTD and ensuring that only the correct tags and attributes appear (and in the right places).

        If an XML document is succesfully parsed by XML::LibXML, then it is well-formed. It sounds like that's all you're interested in checking - therefore you don't need the validity check.

        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: Validating XHTML
by wazoox (Prior) on Apr 28, 2006 at 15:54 UTC
      Ah, this is also something I had not considered. Thanks!
Re: Validating XHTML
by dorward (Curate) on Apr 28, 2006 at 13:31 UTC
    Give it a DTD. I'd use Template-Toolkit to wrap it in a basic document.