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>";
}
?>
$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 クエリに使用する名前空間を指定します。 |
技術詳細
| 戻り値: | 成功した場合は TRUE を返し、失敗した場合は FALSE を返します。 |
|---|---|
| PHP バージョン: | 5.2+ |
PHP SimpleXML リファレンスマニュアル その他の拡張