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>";
}
?>
実行中のインスタンス»
定義と使用法
名前空間コンテキストを作成するために、次のXPathクエリ用registerXPathNamespace()関数。
あなたは、XML文書内の名前空間接頭辞を変更する場合は、この機能が便利です。 registerXPathNamespace()関数は、指定された名前空間接頭辞を作成し、影響を受けたXMLノードは、多くの状況下では、アプリケーション・コードを変更せずにアクセスすることができます。
文法
registerXPathNamespace( prefix , ns );
参数 | 描述 |
---|---|
prefix | 必需。规定在 ns 指定的命名空间的 XPath 查询中使用的的命名空间前缀。 |
ns | 必需。规定用于 XPath 查询的命名空间。 |
技術的詳細
戻り値: | それが成功すればTRUE、失敗時にFALSEを返します。 |
---|---|
PHPバージョン: | 5.2+ |
PHPのSimpleXMLリファレンスマニュアル