php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #70523 check xpath expression
Submitted: 2015-09-17 21:02 UTC Modified: 2021-08-11 14:26 UTC
From: kakkau at grr dot la Assigned: cmb (profile)
Status: Closed Package: DOM XML related
PHP Version: irrelevant OS: any
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: kakkau at grr dot la
New email:
PHP Version: OS:

 

 [2015-09-17 21:02 UTC] 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"; }

?>


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-09-17 21:24 UTC] kakkau at grr dot la
-PHP Version: 5.6.13 +PHP Version: irrelevant
 [2015-09-17 21:24 UTC] 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.
 [2021-08-11 14:26 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2021-08-11 14:26 UTC] cmb@php.net
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>
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri May 09 14:01:27 2025 UTC