php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46764 attributes() not working properly after xpath()
Submitted: 2008-12-05 14:29 UTC Modified: 2009-01-07 12:34 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: maras3000 at gmail dot com Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.2.7 OS: *
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: maras3000 at gmail dot com
New email:
PHP Version: OS:

 

 [2008-12-05 14:29 UTC] maras3000 at gmail dot com
Description:
------------
When looking for list of values of a certain attribute in xml via xpath() method, the returned SimpleXMLElement object's attributes() method does not properly return array of attributes.

Reproduce code:
---------------
test.xml:

<?xml version="1.0"?>
<test>
	<sth attr="1" />
	<sth attr="2" />
	<sth attr="3" />
</test>


index.php:

<pre>
<?php

$xml = simplexml_load_file('./test.xml');
$result = $xml->xpath('//@attr');
foreach($result as $node)
{
	var_dump($node);
	foreach($node->attributes() as $attribute => $value)
		echo $atribute . " = " . (string)$value . "\n";
	echo "---\n";
}
?>
</pre>

Expected result:
----------------
object(SimpleXMLElement)#2 (1) {
  ["@attributes"]=>
  array(1) {
    ["attr"]=>
    string(1) "1"
  }
}
attr = 1
---
object(SimpleXMLElement)#3 (1) {
  ["@attributes"]=>
  array(1) {
    ["attr"]=>
    string(1) "2"
  }
}
attr = 2
---
object(SimpleXMLElement)#4 (1) {
  ["@attributes"]=>
  array(1) {
    ["attr"]=>
    string(1) "3"
  }
}
attr = 3
---


Actual result:
--------------
object(SimpleXMLElement)#2 (1) {
  ["@attributes"]=>
  array(1) {
    ["attr"]=>
    string(1) "1"
  }
}
---
object(SimpleXMLElement)#3 (1) {
  ["@attributes"]=>
  array(1) {
    ["attr"]=>
    string(1) "2"
  }
}
---
object(SimpleXMLElement)#4 (1) {
  ["@attributes"]=>
  array(1) {
    ["attr"]=>
    string(1) "3"
  }
}
---


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-12-05 16:08 UTC] phpwnd at gmail dot com
I think there are two errors in this reproduce code. First, there's a typo in the innermost foreach, $atribute instead of $attribute. Secondly, that XPath expression selects attributes (which explains why attributes() returns NULL, as attributes cannot have attributes), whereas it should select nodes. This code works as expected:

Reproduce code:
---------------
$xml = simplexml_load_file('./test.xml');
foreach ($xml->xpath('//*[@attr]') as $node)
{
	foreach($node->attributes() as $attribute => $value)
		echo $attribute . " = " . (string)$value . "\n";
}
 [2008-12-05 16:33 UTC] maras3000 at gmail dot com
You're right, that's a typo :)

You are also right about attributes not having attributes. But in this case value returned by xpath() should be either array of values or false or sth else, because if you are getting a SimpleXMLElement in which you have [@attributes] which contain what you were looking for and not being able to access them via attributes() method is somewhat wrong IMO.
 [2009-01-07 12:34 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

SimpleXMLElement represents both elements and attributes
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC