mail usprint this pagerss feed

www.liip.ch

Liip is hiring!

registerXPathNamespace() added to SimpleXML

SimpleXML's namespace support is far from ideal, IMHO. But as I don't know an easier solution right now, I don't complain too loud. One of the really missing features (and other people already complained about it) was the possibility to register namespaces/prefixes for xpath operations. You had to either know, which prefix was used for which namespace in the original XML document, or make ugly queries (like "//*[local-name() = 'foo' and namespace-uri() = 'http://foo.com/namespace/']"), especially if there was a default namespace defined... Until today, because I just committed a patch for adding "registerXPathNamespace($prefix,$namespaceURI)" to the SimpleXML extension. It works exactly the same as the same function in the DOMXPath class.

It will be available in PHP 5.1, or from CVS/Snapshots from now on.
Comments (5) |  Permalink

Comments

Isotopp @ 25.07.2004 23:30 CEST
How do you access elements created from external entity references in SimpleXML?

Consider x.xml:




&y;


Here, &y; is an external entity reference to y.xml.

y.xml is

Keks

Now see the sample.php:



Creates output like this:

SimpleXMLElement Object
(
[y] => SimpleXMLElement Object
(
[y] => SimpleXMLElement Object
(
[c] => Keks
)

)

)

How do I get to c?
Isotopp @ 25.07.2004 23:31 CEST
Darn. Again, replace {} with angular brackets.

kris@valiant:~/Source/phpfaq} php sample.php
SimpleXMLElement Object
(
[y] =} SimpleXMLElement Object
(
[y] =} SimpleXMLElement Object
(
[c] =} Keks
)

)

)
kris@valiant:~/Source/phpfaq} cat x.xml
{!DOCTYPE a [
{!ENTITY y SYSTEM "y.xml"}
]}


{a}
&y;
{/a}
kris@valiant:~/Source/phpfaq} cat y.xml
{c}Keks{/c}
kris@valiant:~/Source/phpfaq} cat sample.php
{?php
$xml = simplexml_load_file("x.xml");
print_r($xml);
?}
kris@valiant:~/Source/phpfaq} php sample.php
SimpleXMLElement Object
(
[y] =} SimpleXMLElement Object
(
[y] =} SimpleXMLElement Object
(
[c] =} Keks
)

)

)
kris@valiant:~/Source/phpfaq}
chregu @ 25.07.2004 23:58 CEST
I'm neither the inventor, nor the maintainer and certainly not the promotor of SimpleXML. IMHO, SimpleXML nowadays fails badly on more than just "simple" XML documents. For flat and non-"namespaced" XML documents, it's perfect, for everything else, use the DOM extension (or the xmlReader extension, if you're concerned about memory) . Except having to type less, you gain not much with SimpleXML...

To your question. Looks definitively like a nice bug ;) I have no idea, why and where this happens (my commit today was the first one ever to ext/simplexml by me..), therefore the best thing would be to file a bug report at http://bugs.php.net/

Thanks anyway for the bug report
andrew morton @ 10.01.2006 04:03 CEST
humm... i wish this could be back ported to 5.0. the bug pretty much renders it useless for "simple" things like parsing an Atom RSS feed.
Andreas Åkre Solberg @ 08.01.2008 11:46 CEST
Thank you very much! SimpleXML is useless without namespaces in xpath queries.

Have a nice day.

add a comment

Your email adress will never be published.
Comment spam will be deleted!

For Spammers Only
Name*
E-Mail
URL
Comment*
Notify me via E-Mail when new comments are made to this entry
Remember me (needs cookies)