最新的Web開發教程
 

PHP registerXPathNamespace() Function

<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節點可以在不改變應用程序代碼太多訪問。


句法

registerXPathNamespace( prefix , ns );

參數 描述
prefix 需要。 指定命名空間前綴的XPath查詢使用在NS給出的命名空間
ns 需要。 指定命名空間使用XPath查詢

技術細節

返回值: 如果成功則返回TRUE。 FALSE失敗
PHP版本: 5.2+

<PHP SimpleXML的參考