<?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 => 'zh',
  ),
  'this' => 
  array (
    0 => 'simplexmlelement.registerxpathnamespace.php',
    1 => 'SimpleXMLElement::registerXPathNamespace',
    2 => 'Creates a prefix/ns context for the next XPath query',
  ),
  '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' => 'en',
    '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">
   Creates a prefix/ns context for the next XPath query
  </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">
   Creates a prefix/ns context for the next XPath query. In particular, this is
   helpful if the provider of the given XML document alters the namespace
   prefixes. <code class="literal">registerXPathNamespace</code> will create a prefix for
   the associated namespace, allowing one to access nodes in that namespace 
   without the need to change code to allow for the new prefixes dictated by the
   provider.
  </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">
       The namespace prefix to use in the XPath query for the namespace given in 
       <code class="parameter">namespace</code>.
      </p>
     </dd>
    
    
     <dt><code class="parameter">namespace</code></dt>
     <dd>
      <p class="para">
       The namespace to use for the XPath query. This must match a namespace in
       use by the XML document or the XPath query using 
       <code class="parameter">prefix</code> will not return any results.
      </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 Setting a namespace prefix to use in an XPath query</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>
     Notice how the XML document shown in the example sets a namespace with a 
     prefix of <code class="literal">chap</code>. Imagine that this document (or another
     one like it) may have used a prefix of <code class="literal">c</code> in the past for
     the same namespace. Since it has changed, the XPath query will no longer 
     return the proper results and the query will require modification. Using 
     <code class="literal">registerXPathNamespace</code> avoids future modification of the
     query even if the provider changes the namespace prefix.
    </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> - Runs XPath query on XML data</span></li>
    <li><span class="methodname"><a href="simplexmlelement.getdocnamespaces.php" class="methodname" rel="rdfs-seeAlso">SimpleXMLElement::getDocNamespaces()</a> - Returns namespaces declared in document</span></li>
    <li><span class="methodname"><a href="simplexmlelement.getnamespaces.php" class="methodname" rel="rdfs-seeAlso">SimpleXMLElement::getNamespaces()</a> - Returns namespaces used in document</span></li>
   </ul>
  </p>
 </div>

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