<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Creates a prefix/ns context for the next XPath query</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="simplexmlelement.getnamespaces.html">SimpleXMLElement::getNamespaces</a></div> <div class="next" style="text-align: right; float: right;"><a href="simplexmlelement.savexml.html">SimpleXMLElement::saveXML</a></div> <div class="up"><a href="class.simplexmlelement.html">SimpleXMLElement</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="simplexmlelement.registerxpathnamespace" class="refentry"> <div class="refnamediv"> <h1 class="refname">SimpleXMLElement::registerXPathNamespace</h1> <p class="verinfo">(PHP 5 >= 5.2.0)</p><p class="refpurpose"><span class="refname">SimpleXMLElement::registerXPathNamespace</span> — <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">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">bool</span> <span class="methodname"><strong>SimpleXMLElement::registerXPathNamespace</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$prefix</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$ns</code></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. <em>registerXPathNamespace</em> 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">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">prefix</code></em></span> <dd> <p class="para"> The namespace prefix to use in the XPath query for the namespace given in <em><code class="parameter">ns</code></em>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">ns</code></em></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 <em><code class="parameter">prefix</code></em> will not return any results. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-simplexmlelement.registerxpathnamespace-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> on success or <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 examples" id="refsect1-simplexmlelement.registerxpathnamespace-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-5510"> <p><strong>Example #1 Setting a namespace prefix to use in an XPath query</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$xml </span><span style="color: #007700">= <<<EOD<br /></span><span style="color: #DD0000"><book xmlns:chap="http://example.org/chapter-title"><br /> <title>My Book</title><br /> <chapter id="1"><br /> <chap:title>Chapter 1</chap:title><br /> <para>Donec velit. Nullam eget tellus vitae tortor gravida scelerisque. <br /> In orci lorem, cursus imperdiet, ultricies non, hendrerit et, orci. <br /> Nulla facilisi. Nullam velit nisl, laoreet id, condimentum ut, <br /> ultricies id, mauris.</para><br /> </chapter><br /> <chapter id="2"><br /> <chap:title>Chapter 2</chap:title><br /> <para>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Proin <br /> gravida. Phasellus tincidunt massa vel urna. Proin adipiscing quam <br /> vitae odio. Sed dictum. Ut tincidunt lorem ac lorem. Duis eros <br /> tellus, pharetra id, faucibus eu, dapibus dictum, odio.</para><br /> </chapter><br /></book><br /></span><span style="color: #007700">EOD;<br /><br /></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">);<br /><br /></span><span style="color: #0000BB">$sxe</span><span style="color: #007700">-></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">);<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$sxe</span><span style="color: #007700">-></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 (</span><span style="color: #0000BB">$result </span><span style="color: #007700">as </span><span style="color: #0000BB">$title</span><span style="color: #007700">) {<br /> echo </span><span style="color: #0000BB">$title </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> 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 <em>chap</em>. Imagine that this document (or another one like it) may have used a prefix of <em>c</em> 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 <em>registerXPathNamespace</em> 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">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="methodname"><a href="simplexmlelement.xpath.html" class="methodname" rel="rdfs-seeAlso">SimpleXMLElement::xpath()</a> - Runs XPath query on XML data</span></li> <li class="member"> <span class="methodname"><a href="simplexmlelement.getdocnamespaces.html" class="methodname" rel="rdfs-seeAlso">SimpleXMLElement::getDocNamespaces()</a> - Returns namespaces declared in document</span></li> <li class="member"> <span class="methodname"><a href="simplexmlelement.getnamespaces.html" class="methodname" rel="rdfs-seeAlso">SimpleXMLElement::getNamespaces()</a> - Returns namespaces used in document</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="simplexmlelement.getnamespaces.html">SimpleXMLElement::getNamespaces</a></div> <div class="next" style="text-align: right; float: right;"><a href="simplexmlelement.savexml.html">SimpleXMLElement::saveXML</a></div> <div class="up"><a href="class.simplexmlelement.html">SimpleXMLElement</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>