|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #29243 SimpleXML: add nodeName and localName
Submitted: 2004-07-18 12:54 UTC Modified: 2021-04-14 13:25 UTC
Avg. Score:4.6 ± 0.8
Reproduced:3 of 4 (75.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: jim-bugs dot php dot net at jimdabell dot com Assigned: imsop (profile)
Status: Closed Package: SimpleXML related
PHP Version: 5.0.0 OS: FreeBSD
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: jim-bugs dot php dot net at jimdabell dot com
New email:
PHP Version: OS:


 [2004-07-18 12:54 UTC] jim-bugs dot php dot net at jimdabell dot com
Right now, I don't think it's possible to retrieve the element type name of a simplexml_element instance.  A getElementTypeName() method would be useful.

It would also be useful to have an easier way of retrieving the XML language information, as described in the XML 1.0 specification.

Reproduce code:
Right now I am using a helper function like this to get xml:lang attributes:

function getLanguage($element)
	/* Returns the value of any xml:lang attribute when passed an element or null if there isn't one. */
	$language = null;
	foreach($element->attributes('') as $attributeName => $attribute) {
		if ($attributeName == 'lang') {
			$language = (string)$attribute;

	return $language;

Obviously, that's a little unwieldy, and since the xml:lang attribute is defined in the XML 1.0 specification, it would be nice to have better support for it in PHP.  Right now, as long as they are in the default namespace, it's much easier to get information about arbitrary, undefined attributes than it is about XML 1.0 standard attributes!


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-18 15:49 UTC]
What's getElementTypeName() supposed to return? There's no equivalent in DOM for that... 

If you need more powerful functions/methods/properties, you should maybe use the DOM Extension (or at least convert the node in question to a DOMNode, with dom_import_simplexml($node)). The SimpleXML Extension won't be extended with a lot of new methods/properties, AFAIK. It's supposed to be simple ;) 


 [2004-07-18 15:59 UTC] jim-bugs dot php dot net at jimdabell dot com
getElementTypeName() would return the name of the element type as a string.  For instance, "p", "table", "h1", to use examples from XHTML.

I know about the DOM extension, but I would have thought that, at the very least, getting the element type name and getting standard attribute values would be both frequently used and simple enough to go in.  They are the very first pair of problems I ran into when starting with SimpleXML, so it's hardly an XML expert-only scenario - I'm merely parsing a simple document, nothing advanced.
 [2004-07-18 17:22 UTC]
Ok, it's nodeName or localName then, not ElementTypeName ;)

anyway, at the moment you have to convert a simplexml node to a dom node if you want to have this info

in your example, this woudl look like the following:
$domelement = dom_import_simplexml($element);

return $domelement->getAttributeNS("","lang");

No idea, if and when your wishes will be implemented...

 [2004-08-27 10:29 UTC]
rephrased summary
 [2016-12-30 19:01 UTC]
-Package: Feature/Change Request +Package: SimpleXML related
 [2021-04-14 13:25 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: imsop
 [2021-04-14 13:25 UTC]
The element name can be retrieved with getName() since PHP 5.1.3:

The xml:lang attribute can be retrieved with the following since PHP 5.2.0:

echo $element->attributes('xml', true)->lang;
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 26 13:01:32 2024 UTC