in reply to Validating XHTML

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 ($@) ;

Replies are listed 'Best First'.
Re^2: Validating XHTML
by Anonymous Monk on Apr 28, 2006 at 13:32 UTC
    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

        And then XHTML has the third level of correctness, which is additional checking for things that can't be expressed in the DTD (which is what the validator checks). (Such as the cite attribute for blockquote containing a URL, and that URL pointing to the resource the quote is from).