网络编程
位置:首页>> 网络编程>> php编程>> PHP registerXPathNamespace()函数讲解

PHP registerXPathNamespace()函数讲解

作者:php参考手册  发布时间:2023-06-05 01:58:00 

标签:php,registerxpathnamespace(),函数

PHP registerXPathNamespace() 函数

实例

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


<?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>";
}
?>

定义和用法

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

如果在 XML 文档中改变命名空间前缀,这个函数很有用。registerXPathNamespace()函数将创建一个指定的命名空间前缀,使受影响的 XML 节点可以在不改变应用程序代码太多的情况下进行访问。

语法


registerXPathNamespace( _prefix_ , _ns_ );

PHP registerXPathNamespace()函数讲解

PHP registerXPathNamespace()函数讲解

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com