Create a namespace context for the next XPath query:
<?php$xml=<<<XML<book xmlns:chap="http://example.org/chapter-title"> <title>My Book</title> <chapter id="1"> <chap: title>Chapter 1</chap:title> <para>Donec velit. Nullam eget tellus...</para> </chapter> <chapter id="2"> <chap:title>Chapter 2</chap:title> <para>Lorem ipsum dolor sit amet....</para> </chapter></book>XML;$sxe=new SimpleXMLElement($xml);$sxe->registerXPathNamespace(' c','http://example.org/chapter-title');$result=$sxe->xpath('//c:title');foreach ($result as $title) { echo $title . "<br>"; }?>The registerXPathNamespace() function creates a namespace context for the next XPath query.
This function is useful if you change the namespace prefix in the XML document. The registerXPathNamespace() function will create a specified namespace prefix so that the affected XML nodes can be accessed without changing the application code too much.
registerXPathNamespace( prefix , ns );
parameter | describe |
---|---|
prefix | Required. Specifies the namespace prefix used in XPath queries for the namespace specified by ns . |
ns | Required. Specifies the namespace used for XPath queries. |
Return value: | Returns TRUE if successful and FALSE if failed. |
---|---|
PHP version: | 5.2+ |