#46764 [Opn->Bgs]: attributes() not working properly after xpath()
| From: | jani@php.net | Date: | Mon, 08 Dec 2008 10:51:22 +0000 |
| Subject: | #46764 [Opn->Bgs]: attributes() not working properly after xpath() | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-131737@lists.php.net to get a copy of this message | ||
ID: 46764
Updated by: jani@php.net
Reported By: maras3000 at gmail dot com
-Status: Open
+Status: Bogus
Bug Type: SimpleXML related
Operating System: Windows XP (not checked others)
PHP Version: 5.2.7
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2008-12-05 16:33:42] 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.
------------------------------------------------------------------------
[2008-12-05 16:08:53] 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 14:29:47] 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"
}
}
---
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=46764&edit=1