次の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+ |