<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.simplexmlelement.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'ja',
  ),
  'this' => 
  array (
    0 => 'simplexmlelement.registerxpathnamespace.php',
    1 => 'SimpleXMLElement::registerXPathNamespace',
    2 => '次の XPath クエリ用の prefix/ns コンテキストを作成する',
  ),
  'up' => 
  array (
    0 => 'class.simplexmlelement.php',
    1 => 'SimpleXMLElement',
  ),
  'prev' => 
  array (
    0 => 'simplexmlelement.next.php',
    1 => 'SimpleXMLElement::next',
  ),
  'next' => 
  array (
    0 => 'simplexmlelement.rewind.php',
    1 => 'SimpleXMLElement::rewind',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'ja',
    'path' => 'reference/simplexml/simplexmlelement/registerXPathNamespace.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="simplexmlelement.registerxpathnamespace" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">SimpleXMLElement::registerXPathNamespace</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">SimpleXMLElement::registerXPathNamespace</span> &mdash; <span class="dc-title">
   次の XPath クエリ用の prefix/ns コンテキストを作成する
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-simplexmlelement.registerxpathnamespace-description">
  <h3 class="title">説明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">function</span> <span class="methodname"><strong>SimpleXMLElement::registerXPathNamespace</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$prefix</code></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$namespace</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="para rdfs-comment">
   次の XPath クエリ用の prefix/ns コンテキストを作成します。特にこれが有用なのは、
   XML ドキュメントの提供者が名前空間プレフィックスを変更したような場合です。
   <code class="literal">registerXPathNamespace</code> はプレフィックスを作成して名前空間に関連付け、
   そのプレフィックスで名前空間のノードにアクセスできるようにします。
   提供者側がプレフィックスを変更したとしても、コードを書き換える必要はありません。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-simplexmlelement.registerxpathnamespace-parameters">
  <h3 class="title">パラメータ</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">prefix</code></dt>
     <dd>
      <p class="para">
       <code class="parameter">ns</code> で指定した名前空間への XPath クエリで使用する、
       名前空間プレフィックス。
      </p>
     </dd>
    
    
     <dt><code class="parameter">ns</code></dt>
     <dd>
      <p class="para">
       XPath クエリで使用する名前空間。
       これは XML ドキュメントで使用している名前空間と一致していなければなりません。
       一致していない場合、<code class="parameter">prefix</code>
       を使用した XPath クエリは何も結果を返しません。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-simplexmlelement.registerxpathnamespace-returnvalues">
  <h3 class="title">戻り値</h3>
  <p class="para">
   成功した場合に <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> を、失敗した場合に <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> を返します。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-simplexmlelement.registerxpathnamespace-examples">
  <h3 class="title">例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>例1 XPath クエリで使用する名前空間プレフィックスの設定</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php

$xml </span><span style="color: #007700">= &lt;&lt;&lt;EOD
</span><span style="color: #DD0000">&lt;book xmlns:chap="http://example.org/chapter-title"&gt;
    &lt;title&gt;My Book&lt;/title&gt;
    &lt;chapter id="1"&gt;
        &lt;chap:title&gt;Chapter 1&lt;/chap:title&gt;
        &lt;para&gt;Donec velit. Nullam eget tellus vitae tortor gravida scelerisque. 
            In orci lorem, cursus imperdiet, ultricies non, hendrerit et, orci. 
            Nulla facilisi. Nullam velit nisl, laoreet id, condimentum ut, 
            ultricies id, mauris.&lt;/para&gt;
    &lt;/chapter&gt;
    &lt;chapter id="2"&gt;
        &lt;chap:title&gt;Chapter 2&lt;/chap:title&gt;
        &lt;para&gt;Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Proin 
            gravida. Phasellus tincidunt massa vel urna. Proin adipiscing quam 
            vitae odio. Sed dictum. Ut tincidunt lorem ac lorem. Duis eros 
            tellus, pharetra id, faucibus eu, dapibus dictum, odio.&lt;/para&gt;
    &lt;/chapter&gt;
&lt;/book&gt;
</span><span style="color: #007700">EOD;

</span><span style="color: #0000BB">$sxe </span><span style="color: #007700">= new </span><span style="color: #0000BB">SimpleXMLElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$xml</span><span style="color: #007700">);

</span><span style="color: #0000BB">$sxe</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">registerXPathNamespace</span><span style="color: #007700">(</span><span style="color: #DD0000">'c'</span><span style="color: #007700">, </span><span style="color: #DD0000">'http://example.org/chapter-title'</span><span style="color: #007700">);
</span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$sxe</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">xpath</span><span style="color: #007700">(</span><span style="color: #DD0000">'//c:title'</span><span style="color: #007700">);

foreach (</span><span style="color: #0000BB">$result </span><span style="color: #007700">as </span><span style="color: #0000BB">$title</span><span style="color: #007700">) {
  echo </span><span style="color: #0000BB">$title </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;
}

</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
    </div>

    <div class="example-contents"><p>上の例の出力は以下となります。</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Chapter 1
Chapter 2</pre>
</div>
    </div>
    <div class="example-contents"><p>
     上の例の XML ドキュメントでは、プレフィックス <code class="literal">chap</code>
     で名前空間を指定していることを確認しておきましょう。仮に、このドキュメント
     (あるいはよく似た別のドキュメント) が以前に同じ名前空間に対してプレフィックス
     <code class="literal">c</code> を使用していたとしましょう。プレフィックスが変わった時点で、
     これまでの XPath クエリは正しい値を返さないようになります。
     そしてクエリに対して何らかの変更が必要となります。
     <code class="literal">registerXPathNamespace</code> を使用すると、
     仮に名前空間プレフィックスが変更された場合でもクエリの変更する必要がなくなります。
    </p></div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-simplexmlelement.registerxpathnamespace-seealso">
  <h3 class="title">参考</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="methodname"><a href="simplexmlelement.xpath.php" class="methodname" rel="rdfs-seeAlso">SimpleXMLElement::xpath()</a> - XML データに Xpath クエリを実行する</span></li>
    <li><span class="methodname"><a href="simplexmlelement.getdocnamespaces.php" class="methodname" rel="rdfs-seeAlso">SimpleXMLElement::getDocNamespaces()</a> - ドキュメントで宣言されている名前空間を返す</span></li>
    <li><span class="methodname"><a href="simplexmlelement.getnamespaces.php" class="methodname" rel="rdfs-seeAlso">SimpleXMLElement::getNamespaces()</a> - ドキュメントで使用している名前空間を返す</span></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>