php.net |  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
Votes:5
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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
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
Description:
------------
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('http://www.w3.org/XML/1998/namespace') 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!


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-18 15:49 UTC] chregu@php.net
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 ;) 

chregu

 [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] chregu@php.net
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("http://www.w3.org/XML/1998/namespace","lang");
***

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


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

https://www.php.net/manual/en/simplexmlelement.getname.php

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: Tue Mar 19 11:01:28 2024 UTC