PHP registerXPathNamespace() Function

In this chapter you will learn:

  1. Description for PHP registerXPathNamespace() Function
  2. Syntax for PHP registerXPathNamespace() Function
  3. Parameter for PHP registerXPathNamespace() Function
  4. Return for PHP registerXPathNamespace() Function
  5. Example - Create a namespace context for the next XPath query

Description

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

Syntax

PHP registerXPathNamespace() Function has the following syntax.

registerXPathNamespace(prefix,ns);

Parameter

ParameterIs RequiredDescription
prefixRequired.Namespace prefix to use in the XPath query for the namespace given in ns
nsRequired.Namespace to use for the XPath query

Return

Returns TRUE on success. FALSE on failure

Example

Create a namespace context for the next XPath query:


<?php//from  jav a 2 s .c om
$xml=<<<XML
<book xmlns:chap="http://example.org/chapter-title">
  <title>My Book</title>
  <chapter id="1">
    <chap:title>Chapter 1</chap:title>
    <para>PHP Data Types</para>
  </chapter>
  <chapter id="2">
    <chap:title>Chapter 2</chap:title>
    <para>PHP Statements</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 . "\n";
}
?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Description for PHP simplexml_import_dom() Function
  2. Syntax for PHP simplexml_import_dom() Function
  3. Parameter for PHP simplexml_import_dom() Function
  4. Return for PHP simplexml_import_dom() Function
  5. Example - Take a node of a DOM document and make it into a SimpleXML node
Home » PHP Tutorial » PHP SimpleXML Functions
PHP SimpleXMLElement Create
PHP addAttribute() Function
PHP addChild() Function
PHP asXML() Function
PHP attributes() Function
PHP children() Function
PHP SimpleXML count() Function
PHP getDocNamespaces() Function
PHP getName() Function
PHP getNamespaces() Function
PHP registerXPathNamespace() Function
PHP simplexml_import_dom() Function
PHP simplexml_load_file() Function
PHP simplexml_load_string() Function
PHP xpath() Function