PHP registerXPathNamespace () function
PHP SimpleXML Reference Manual
Examples
For the next XPath query to create a namespace context:
<?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>";
}
?>
$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>";
}
?>
Running instance »
Definition and Usage
registerXPathNamespace () function for the next XPath query to create a namespace context.
If you change the namespace prefix in the XML document, this function is useful. registerXPathNamespace () function will create a namespace prefix specified, the affected XML nodes can be accessed without changing the application code under many circumstances.
grammar
registerXPathNamespace( prefix , ns );
参数 | 描述 |
---|---|
prefix | 必需。规定在 ns 指定的命名空间的 XPath 查询中使用的的命名空间前缀。 |
ns | 必需。规定用于 XPath 查询的命名空间。 |
technical details
return value: | If successful it returns TRUE, on failure returns FALSE. |
---|---|
PHP version: | 5.2+ |
PHP SimpleXML Reference Manual