Req #29243 [Opn->Csd]: SimpleXML: add nodeName and localName
Edit report at https://bugs.php.net/bug.php?id=29243&edit=1
ID: 29243
Updated by: imsop@php.net
Reported by: jim-bugs dot php dot net at jimdabell dot com
Summary: SimpleXML: add nodeName and localName
-Status: Open
+Status: Closed
Type: Feature/Change Request
Package: SimpleXML related
Operating System: FreeBSD
PHP Version: 5.0.0
-Assigned To:
+Assigned To: imsop
Block user comment: N
Private report: N
New Comment:
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;
Previous Comments:
------------------------------------------------------------------------
[2004-08-27 10:29:59] chregu@php.net
rephrased summary
------------------------------------------------------------------------
[2004-07-18 17:22:50] 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-07-18 15:59:44] 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 15:49:36] 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 12:54:14] 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!
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=29243&edit=1
Thread (6 messages)