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ノードにアプリケーションコードをあまり変更することなくアクセスできます。


構文

registerXPathNamespace(prefix,ns);

パラメータ 説明
prefix 必須。nsで指定した、名前空間のXPathクエリで使用する名前空間の接頭辞を指定する
ns 必須。XPathクエリに使用する名前空間を指定する

技術内容
返り値: Returns 成功した場合は TRUEを、失敗した場合は FALSEを返します
PHP バージョン: 5.2+

❮ PHP SimpleXML リファレンス