|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #30814 more detailed feedback from DOMDocument::schemaValidate
Submitted: 2004-11-17 16:55 UTC Modified: 2022-04-08 08:07 UTC
Avg. Score:4.8 ± 0.4
Reproduced:5 of 5 (100.0%)
Same Version:3 (60.0%)
Same OS:1 (20.0%)
From: pecoes at web dot de Assigned: ilutov (profile)
Status: Closed Package: *General Issues
PHP Version: 5.0.2 OS: windows xp
Private report: No CVE-ID: None
 [2004-11-17 16:55 UTC] pecoes at web dot de
A lot can go wrong during schema validation:

1.) the schema cannot be opened
2.) the schema is not well-formed
3.) the schema itself is invalid
4.) the $dom in question is invalid

Currently there is neither a way to distinguish between these four problems, nor a way to capture an actual validation error(4). Imho schemaValidate really needs some refinements. Here's an idea:

schemaValidate (string file [, integer errType [, string errMsg]])

errType and errMsg would describe the error (if any) and schemaValidate would STOP triggering errors and return false instead. Usage:

if (!$dom->schemaValidate($schema, $errType, $errMsg)) {
    switch ($errType) {
        case SCHEMA_NOT_FOUND:
            throw new Exception("$schema not found");
            throw new Exception("$schema not well-formed");
        case SCHEMA_INVALID:
            throw new Exception("$schema is invalid");
        case SCHEMA_DOM_INVALID:
            throw new Exception("XML invalid: $errMsg");

or simply:

if (!$dom->schemaValidate($schema, $errType, $errMsg)) {
    throw new Exception($errMsg ? $errMsg : "$schema not usable");

Whether you like my idea or not, I hope you'll agree that:

if (!@$dom->schemaValidate($schema)) {
    throw new Exception("cannot validate or invalid input");

is not enough.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2004-12-06 08:11 UTC] anthony at ectrolinux dot com
I second that this issue needs to be addressed, as it's quite tiresome to code work-arounds for this problem. While not the most elegant solution, the errors DOM emits when validating an XML document against a schema can be captured, parsed and processed at the script level to emulate the functionality you described. 

From an implementational standpoint, I agree with your idea in principle, though I believe that a few modifications are necessary:

Let's assume that a user is going to design an XML Schema to validate user-submitted form data, which will be validated with DOM (using XPath): this data will be grouped into a set of elements, and then validated. 

In order to best represent the errors for this scenario, it would be helpful for DOM to throw a few basic pieces information, including: The parent path of the faulty element, the name of the element, a description of the error and the error -code-. In place of an error -number-, using the W3C-defined error codes for the XML Schema Structures specification would provide a consistant means of reporting invalidities. You could then, as you demonstrated, use a simple switch statement to process the errors as needed.

Though I believe the above would be well suited to addressing the problem outlined by this bug, as mentioned in the above scenario, it would also be helpful to know only the elements that -failed- to validate. A method of "reverse-validating" a document would be quite helpful, in that it would simply report the elements that failed to validate. This could be done at the script level if the functionality was not provided natively by PHP.
 [2022-04-08 08:07 UTC]
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: ilutov
 [2022-04-08 08:07 UTC]
This issue is very old. If it's still relevant please create a new issue on GitHub.
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Mon Dec 05 11:05:53 2022 UTC