Req #70523 [Opn->Csd]: check xpath expression

From: Date: Wed, 11 Aug 2021 14:26:15 +0000
Subject: Req #70523 [Opn->Csd]: check xpath expression
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-235770@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70523&edit=1 ID: 70523 Updated by: cmb@php.net Reported by: kakkau at grr dot la Summary: check xpath expression -Status: Open +Status: Closed Type: Feature/Change Request Package: DOM XML related Operating System: any PHP Version: irrelevant -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: PHP warns about invalid xpath expressions, and you can retrieve this info via error_get_last()[1]. As such it is already possible to distinguish invalid expressions from valid expressions evaluating to false. [1] <https://3v4l.org/fgMTl> Previous Comments: ------------------------------------------------------------------------ [2015-09-17 21:24:51] kakkau at grr dot la php version irrelevant as the return value on failures is false. should be a unique one or provide a function to compensate this behaviour. ------------------------------------------------------------------------ [2015-09-17 21:02:29] kakkau at grr dot la Description: ------------ --- From manual page: http://www.php.net/class.domxpath --- in some cases the return value of DOMXPath::evaluate could be unfavorable. For example what if a valid xpath expression like 'false()' or 'count(...)=0' evaluates to false and you want to catch e.g. malformed xpath expressions which return false as well? how to distinguish those scenarios? therefor it would be useful to have a function to - check the xpath expression first or - having some kind of unique value returned in case the evaluation failed Test script: --------------- <?php $d = new DOMDocument(); $xs = '<?xml version="1.0" ?><root><foo /></root>'; $d->loadXML($xs); $x = new DOMXPath($d); // simple eval $eval = $x->evaluate('false()'); if(!$eval) { echo "BOOBOO"; } // complex but valid eval $eval = $x->evaluate('count(//foo)=0'); if(!$eval) { echo "BOOBOO"; } // bad eval $eval = $x->evaluate(')'); if(!$eval) { echo "BOOBOO"; } ?> ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=70523&edit=1

« previous php.bugs (#235770) next »