function.simplexml-element-registerxpathnamespace.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 136 行

HTML
136
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Creates a prefix/ns context for the next XPath query</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.simplexml-element-getNamespaces.html">SimpleXMLElement->getNamespaces</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.simplexml-element-xpath.html">SimpleXMLElement->xpath</a></div> <div class="up"><a href="ref.simplexml.html">SimpleXML Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.simplexml-element-registerXPathNamespace" class="refentry"> <div class="refnamediv">  <h1 class="refname">SimpleXMLElement-&gt;registerXPathNamespace</h1>  <p class="verinfo">(No version information available, might be only in CVS)</p><p class="refpurpose"><span class="refname">SimpleXMLElement-&gt;registerXPathNamespace</span> &mdash; <span class="dc-title">   Creates a prefix/ns context for the next XPath query  </span></p> </div> <div class="refsect1 description">  <h3 class="title">Description</h3>  <div class="classsynopsis">   <div class="ooclass"><b class="classname">SimpleXMLElement</b></div>   <div class="methodsynopsis dc-description">    <span class="type">bool</span> <span class="methodname"><b><b>registerXPathNamespace</b></b></span>     ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$prefix</tt></span>    , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$ns</tt></span>    )</div>  </div>  <p class="para">   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. <i>registerXPathNamespace</i> 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">  <h3 class="title">Parameters</h3>  <p class="para">   <dl>    <dt>     <span class="term"><i><tt class="parameter">prefix</tt></i></span>     <dd>      <p class="para">       The namespace prefix to use in the XPath query for the namespace given in        <i><tt class="parameter">ns</tt></i>.      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">ns</tt></i></span>     <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        <i><tt class="parameter">prefix</tt></i> will not return any results.      </p>     </dd>    </dt>   </dl>  </p> </div> <div class="refsect1 returnvalues">  <h3 class="title">Return Values</h3>  <p class="para">   Returns <b><tt>TRUE</tt></b> on success or <b><tt>FALSE</tt></b> on failure.  </p> </div> <div class="refsect1 examples">  <h3 class="title">Examples</h3>  <p class="para">   <div class="example">    <p><b>Example #1 Setting a namespace prefix to use in an XPath query</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$xml&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;EOD<br /></span><span style="color: #0000BB">&lt;book&nbsp;xmlns:chap="http://example.org/chapter-title"&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;My&nbsp;Book&lt;/title&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;chapter&nbsp;id="1"&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;chap:title&gt;Chapter&nbsp;1&lt;/chap:title&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;para&gt;Donec&nbsp;velit.&nbsp;Nullam&nbsp;eget&nbsp;tellus&nbsp;vitae&nbsp;tortor&nbsp;gravida&nbsp;scelerisque.&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In&nbsp;orci&nbsp;lorem,&nbsp;cursus&nbsp;imperdiet,&nbsp;ultricies&nbsp;non,&nbsp;hendrerit&nbsp;et,&nbsp;orci.&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Nulla&nbsp;facilisi.&nbsp;Nullam&nbsp;velit&nbsp;nisl,&nbsp;laoreet&nbsp;id,&nbsp;condimentum&nbsp;ut,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ultricies&nbsp;id,&nbsp;mauris.&lt;/para&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;/chapter&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;chapter&nbsp;id="2"&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;chap:title&gt;Chapter&nbsp;2&lt;/chap:title&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;para&gt;Lorem&nbsp;ipsum&nbsp;dolor&nbsp;sit&nbsp;amet,&nbsp;consectetuer&nbsp;adipiscing&nbsp;elit.&nbsp;Proin&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gravida.&nbsp;Phasellus&nbsp;tincidunt&nbsp;massa&nbsp;vel&nbsp;urna.&nbsp;Proin&nbsp;adipiscing&nbsp;quam&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vitae&nbsp;odio.&nbsp;Sed&nbsp;dictum.&nbsp;Ut&nbsp;tincidunt&nbsp;lorem&nbsp;ac&nbsp;lorem.&nbsp;Duis&nbsp;eros&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tellus,&nbsp;pharetra&nbsp;id,&nbsp;faucibus&nbsp;eu,&nbsp;dapibus&nbsp;dictum,&nbsp;odio.&lt;/para&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;/chapter&gt;<br />&lt;/book&gt;<br /></span><span style="color: #007700">EOD;<br /><br /></span><span style="color: #0000BB">$sxe&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SimpleXMLElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$xml</span><span style="color: #007700">);<br /><br /></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">,&nbsp;</span><span style="color: #DD0000">'http://example.org/chapter-title'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</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">);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$title</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$title&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>    <div class="example-contents"><p>     Notice how the XML document shown in the example sets a namespace with a      prefix of <i>chap</i>. Imagine that this document (or another     one like it) may have used a prefix of <i>c</i> 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      <i>registerXPathNamespace</i> avoids future modification of the     query even if the provider changes the namespace prefix.    </p></div>   </div>  </p> </div> <div class="refsect1 seealso">  <h3 class="title">See Also</h3>  <p class="para">   <ul class="simplelist">    <li class="member"><a href="function.simplexml-element-getDocNamespaces.html" class="xref">SimpleXMLElement->getDocNamespaces</a></li>    <li class="member"><a href="function.simplexml-element-getNamespaces.html" class="xref">SimpleXMLElement->getNamespaces</a></li>   </ul>  </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.simplexml-element-getNamespaces.html">SimpleXMLElement->getNamespaces</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.simplexml-element-xpath.html">SimpleXMLElement->xpath</a></div> <div class="up"><a href="ref.simplexml.html">SimpleXML Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?