php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44547 Problem with starts-with in XPath
Submitted: 2008-03-27 11:43 UTC Modified: 2008-03-28 14:08 UTC
From: s_nutchanat at hotmail dot com Assigned:
Status: Not a bug Package: DOM XML related
PHP Version: 5.2.5 OS: Linux
Private report: No CVE-ID: None
 [2008-03-27 11:43 UTC] s_nutchanat at hotmail dot com
Description:
------------
I would like to query my XML file with XPath using starts-with.
This is an example of my XML file:
<?xml version="1.0" encoding="utf-8"?>
<graphml>
  <graph id="G" edgedefault="directed">
    <node id="4">
      <data key="label">name</data>
      <data key="value">Alanine</data>
      <data key="node_path">moldb.molecule.name</data>
    </node>
  </graph>
</graphml>

I want to query to the node having the "node_path" attribute beginning by "moldb". But this didn't work. After trying many cases, I think it came from the XML structure that has more than one "data" nodes. If there is only one "data" node, it worked.

Reproduce code:
---------------
Here is my code:
$doc = new DomDocument(); // document to be explored
if (!$doc->load("ex.xml"))
{
	return; // invalid file
}
$xpath = new DOMXPath($doc); // to search for the nodes we want
$data = 'moldb';
$query = "/graphml/graph/node[starts-with(data,'$data')]";
$query = "/graphml/graph/node[data[@key='node_path'] and starts-with(data,'$data')]";
$entries = $xpath->evaluate($query);
echo $entries->length."\n";

Expected result:
----------------
When executing this script, it should print:
1

Actual result:
--------------
The obtained result is:
0

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-03-28 14:08 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

data converted to string is concat of all text nodes of all the data 
nodes. Bad query
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 17 23:01:32 2024 UTC