registerXPathNamespace()函数为下一个XPath查询创建命名空间上下文。

 

语法

PHP registerXPathNamespace()函数具有以下语法。

registerXPathNamespace(prefix,ns);

 

参数

参数 是否必须 描述
字首 需要。 在ns中给定的命名空间的XPath查询中使用的命名空间前缀
ns 需要。 用于XPath查询的名称空间

 

返回值

成功时返回TRUE。失败时为FALSE

 

实例

为下一个XPath查询创建命名空间上下文:

<?php
//  http://www.manongjc.com/article/1626.html
$ =<<< 
<book  ns:chap=\"http://example.org/chapter- \">
  < >My Book</ >
  <chapter id=\"1\">
    <chap: >Chapter 1</chap: >
    <para>PHP Data Types</para>
  </chapter>
  <chapter id=\"2\">
    <chap: >Chapter 2</chap: >
    <para>PHP Statements</para>
  </chapter>
</book>
 ;

$sxe=new Simple Element($ );
$sxe->registerXPathNamespace(\'c\',\'http://example.org/chapter- \');
$result=$sxe->xpath(\'//c: \');
foreach ($result as $ ){
  echo $  . \"\\n\";
}
?>

上面的代码生成以下结果。

Chapter 1 
Chapter 2
收藏 打印