php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43771 validateOnParse & validate() give difference results
Submitted: 2008-01-06 17:18 UTC Modified: 2008-01-23 11:24 UTC
From: missingno at ifrance dot com Assigned:
Status: Not a bug Package: DOM XML related
PHP Version: 5.3CVS-2008-01-06 (snap) OS: Ubuntu 7.10
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
28 - 10 = ?
Subscribe to this entry?

 
 [2008-01-06 17:18 UTC] missingno at ifrance dot com
Description:
------------
From what I understand, DOMDocument::validateOnParse() = TRUE & DOMDocument::validate() should return the same list of errors for a given document.

But when dealing with HTML code, validate() seems to fail to deal with the DTD correctly.

Therefore, using validate() & validateOnParse gives different results.

I think that in the case of validate(), PHP calls libxml with options that make it think it will be dealing with XML code and thus, an XML DTD. So, once libxml loads the HTML DTD, it fails to parse it correctly and returns a bunch of errors.

(HTML & XML DTDs are similar, except that HTML ones allow for some more constructs like the '&' operator in ELEMENT declarations)

Reproduce code:
---------------
<?php
/* Note: removing the system identifier doesn't change the result,
 * except that "errors" in the DTD are caught immediately.
 * (My guess would be that libxml tries to fetch the DTD from the
 * system identifier instead of using a catalog for resolution) */
$markup = <<<HTML
<!DOCTYPE HTML PUBLIC
    "-//W3C//DTD HTML 4.0 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head><title>Foo</title></head>
    <body><p>Bar</p></body>
</html>
HTML;

header('Content-Type: text/plain');
libxml_use_internal_errors(TRUE);

// First, using DOMDocument::validateOnParse.
libxml_clear_errors();
$dd1 = new DOMDocument();
$dd1->validateOnParse      = TRUE;

echo "Using validateOnParse:\n";
$dd1->loadHTML($markup);
var_dump(libxml_get_errors());

echo "\n\n";

// Now, using DOMDocument::validate() instead.
libxml_clear_errors();
$dd2 = new DOMDocument();
$dd2->validateOnParse      = FALSE;

echo "Using validate():\n";
$dd2->loadHTML($markup);
$dd2->validate();
var_dump(libxml_get_errors());

?>

Expected result:
----------------
Using validateOnParse:
array(0) {
}


Using validate():
array(0) {
}


Actual result:
--------------
Using validateOnParse:
array(0) {
}


Using validate():
array(3) {
  [0]=>
  object(LibXMLError)#3 (6) {
    ["level"]=>
    int(3)
    ["code"]=>
    int(37)
    ["column"]=>
    int(1)
    ["message"]=>
    string(55) "xmlParseEntityDecl: entity HTML.Version not terminated
"
    ["file"]=>
    string(36) "http://www.w3.org/TR/html4/loose.dtd"
    ["line"]=>
    int(31)
  }
  [1]=>
  object(LibXMLError)#4 (6) {
    ["level"]=>
    int(3)
    ["code"]=>
    int(60)
    ["column"]=>
    int(1)
    ["message"]=>
    string(37) "Content error in the external subset
"
    ["file"]=>
    string(36) "http://www.w3.org/TR/html4/loose.dtd"
    ["line"]=>
    int(31)
  }
  [2]=>
  object(LibXMLError)#5 (6) {
    ["level"]=>
    int(2)
    ["code"]=>
    int(517)
    ["column"]=>
    int(0)
    ["message"]=>
    string(74) "Could not load the external subset "http://www.w3.org/TR/html4/loose.dtd"
"
    ["file"]=>
    string(0) ""
    ["line"]=>
    int(0)
  }
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-01-23 11:24 UTC] rrichards@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

validation only works for XML data, so XHTML DOCTYPE needed.
Also, in order to use validateOnParse, you need to load via loadXML()
 [2012-05-20 08:39 UTC] it dot registrations at symphony-group dot co dot uk
I feel this is a bug, and it still exists as of PHP 5.3.8 on OS X 10.7.3 and PHP 
5.4.3 compiled on Fedora 15
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 08:01:28 2024 UTC