Current Location: Home> Function Categories> registerXPathNamespace

registerXPathNamespace

Creates a namespace context for the next XPath query.
Name:registerXPathNamespace
Category:Uncategorized
Programming Language:php
One-line Description:Creates a namespace context for the next XPath query.

Definition and usage

registerXPathNamespace() function creates a namespace context for the next XPath query.

This function is useful if the namespace prefix changes in the XML document. registerXPathNamespace() function creates a prefix for the specified namespace, allowing the affected XML node to be accessed without extensive modification of the application code.

Example

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>" ;
  }
?>

Run the instance

grammar

 SimpleXMLElement :: registerXPathNamespace ( prefix , ns )
parameter describe
prefix Required. Specifies the prefix used in the XPath query for the given namespace in ns .
ns Required. Specifies the namespace used for XPath query.