note 35582 added to function.xpath-eval

From: Date: Sat, 06 Sep 2003 17:52:41 +0000
Subject: note 35582 added to function.xpath-eval
Groups: php.notes 
Request: Send a blank email to php-notes+get-55554@lists.php.net to get a copy of this message
In order to use the default namespace you must understand how namespace prefixes work. Prefixes are simply convenient mappies to the namespace URI. For example, if you set the namespace: xmlns:xm="http://www.someurl.org" and you have the following document fragment: <rootnode><xm:childnode>Text</xm:childnode></rootnode> this is essentially equivalent to: <rootnode> <http://www.someurl.org:childnode> Text </http://www.someurl.org:childnode> </rootnode> because the namespace URI is what matters, not the namespace prefix. Unfortuantly, if you have a default namespace: xmlns="http://www.anotherurl.org" then all elements without a prefix belong to that namespace, and yet, it appears that PHP, and the underlying LIBXML2 don't let you register a default namespace with "xpath_register_ns(context, prefix, uri)" i.e. by leaving the prefix = "". Therefore, to get around the problem, simply give the default prefix a simple name, such as "pre". For example, if you have a default namespace declaration such as the following document: <?xml version="1.0" encoding="UTF-8"?> <rootname xmlns="http://www.some.org" xml:lang="en-US"> <childnode>Some text</childnode> </rootname> And you want to evaluate the xpath expression: "/rootname/childnode" then you need to register the default namespace in PHP like this: xpath_register_ns(context, "pre", "http://www.some.org"); and then use the following xpath expression: "/pre:rootname/pre:childnode" As you can see this is a lot prettier and more intuititive than using the local-name() function. In addition, it makes your code more portable, because you are guaranteed to always be working on nodes that belong to your explicitly stated namespace, uniquely identified by your URI. ---- Manual Page -- http://www.php.net/manual/en/function.xpath-eval.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35582 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35582&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35582&report=yes

« previous php.notes (#55554) next »