PHP registerXPathNamespace() Function
In this chapter you will learn:
- Description for PHP registerXPathNamespace() Function
- Syntax for PHP registerXPathNamespace() Function
- Parameter for PHP registerXPathNamespace() Function
- Return for PHP registerXPathNamespace() Function
- 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
Parameter | Is Required | Description |
---|---|---|
prefix | Required. | Namespace prefix to use in the XPath query for the namespace given in ns |
ns | Required. | 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:
- Description for PHP simplexml_import_dom() Function
- Syntax for PHP simplexml_import_dom() Function
- Parameter for PHP simplexml_import_dom() Function
- Return for PHP simplexml_import_dom() Function
- Example - Take a node of a DOM document and make it into a SimpleXML node
Home » PHP Tutorial » PHP SimpleXML Functions