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
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:
2 + 33 = ?
Subscribe to this entry?

 
 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 04:01:30 2024 UTC