目录

PHP registerXPathNamespace() 函数

❮ PHP SimpleXML 参考

示例

为下一个 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 节点,而无需过多更改应用程序代码。


语法

SimpleXMLElement::registerXPathNamespace( prefix, ns)

参数值

Parameter Description
prefix Required. Specifies the namespace prefix to use in the XPath query for the namespace given in ns
ns Required. Specifies the namespace to use for the XPath query

技术细节

返回值: 成功则为真。失败时为 FALSE
PHP 版本: 5.1+

❮ PHP SimpleXML 参考