php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50759 SimpleXML ::atrributes() feature does not recognize hyphenated attributes
Submitted: 2010-01-14 23:19 UTC Modified: 2010-01-15 01:56 UTC
From: stevendamico at gmail dot com Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.2.12 OS: Windows
Private report: No CVE-ID: None
 [2010-01-14 23:19 UTC] stevendamico at gmail dot com
Description:
------------
Using SimpleXML and SimpleXML_load_file to load an external XML file. 
The ::attributes feature does not recognize hyphens in attribute 
features, no documentation on how to resolve feature and the solution 
for dealing with hyphens in nodes (by putting it in curly braces and 
quotes {''}) does not solve the problem. 

This is my first time submitting a bug and I'm an amateur, but there 
is no documentation for a solution. Not sure if I selected the proper 
bug category. Sorry for any mistakes.  









Reproduce code:
---------------
---
From manual page: simplexmlelement.attributes#Description
---

portion of xml file (parent nodes left out for simplicity):

<recorded-vote><legislator name-id="A000014" sort-field="Abercrombie" unaccented-name="Abercrombie" party="D" state="HI" role="legislator">Abercrombie</legislator><vote>Aye</vote></recorded-vote> 

ex. 1: 

echo {'recorded-vote'}[0]->legislator->attributes()->{'name-id'}; 

ex. 2: 

print_r ({'recorded-vote'}[0]->legislator->attributes());





Expected result:
----------------
ex. 1

A000014

ex. 2

SimpleXMLElement Object ( [@attributes] => Array ( [name-id] => A000014 
[sort-field] => Abercrombie [unaccented-name] => Abercrombie [party] => 
D [state] => HI [role] => legislator ) ) 


Actual result:
--------------
ex. 1

0 

To clarify, all that outputs is the digit zero. 

ex. 2

SimpleXMLElement Object ( [@attributes] => Array ( [party] => D [state] 
=> HI [role] => legislator ) ) 


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-01-15 01:55 UTC] aharvey@php.net
Per example #5 in http://au2.php.net/manual/en/simplexml.examples-basic.php, the idiomatic way to access attributes in a SimpleXML tree is to access them in the same way as array elements.

In your example, this should work (and works for me):

echo $doc->...->{'recorded-vote'}->legislator['name-id'];

Closing, as this isn't a bug in PHP itself. Thanks for your interest in PHP!
 [2010-01-15 01:56 UTC] aharvey@php.net
For the record, that link should have been to http://au2.php.net/manual/en/simplexml.examples-basic.php

(Insert ritual complaint about bug tracker auto-linkification here.)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 06:01:32 2024 UTC