最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
PHP registerXPathNamespace()函数讲解
时间:2026-05-30 20:30:01 编辑:袖梨 来源:一聚教程网
PHP registerXPathNamespace() 函数
实例
为下一个 XPath 查询创建命名空间上下文:
<?php $xml=<<<XML<book xmlns:chap="http://e*xa*mpl*e.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://e*xa*mpl*e.org/chapter-title'); $result=$sxe->xpath('//c:title'); foreach ($result as $title) { echo $title . "<br>"; } ?>
定义和用法
registerXPathNamespace()函数为下一个 XPath 查询创建命名空间上下文。
如果在 XML 文档中改变命名空间前缀,这个函数很有用。registerXPathNamespace()函数将创建一个指定的命名空间前缀,使受影响的 XML 节点可以在不改变应用程序代码太多的情况下进行访问。
语法
registerXPathNamespace( _prefix_ , _ns_ );


总结