您现在的位置是:首页 > PHP函数

李清波 2021-06-17 PHP函数 1563 复制当前网址

registerXPathNamespace



PHP registerXPathNamespace() 函数

定义和用法

registerXPathNamespace() 函数为下一次 XPath 查询创建命名空间语境。


语法

class SimpleXMLElement
{
string registerXPathNamespace(prefix,ns)
}


参数描述
prefix必需。规定命名空间前缀。
ns必需。规定命名空间 URL。必须匹配 XML 文档中的命名空间。


例子

XML 文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note xmlns:b="http://www.w3school.com.cn/example/">
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<b:body>Dont forget the meeting!</b:body>
</note>

PHP 代码:

<?php
$xml = simplexml_load_file("test.xml");$xml->registerXPathNamespace("msg", "http://www.w3school.com.cn/example/");$result = $xml->xpath("msg:body");

foreach ($result as $message)
  {
  echo $message;
  }
?>

输出类似:

Dont forget the meeting!


文章来源:https://liqingbo.com/blog-793.html

上一篇:simplexml_import_dom

下一篇:getNamespace

评论