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>";
}
?>
定義と使用法
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 |
技術的な詳細
戻り値: | 成功するとTRUE。失敗するとFALSE |
---|---|
PHPバージョン: | 5.1+ |
❮PHPSimpleXMLリファレンス