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->registerXPathNamespace</h1> <p class="verinfo">(No version information available, might be only in CVS)</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"> <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"><?php<br /><br />$xml </span><span style="color: #007700">= <<<EOD<br /></span><span style="color: #0000BB"><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> 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 + -
显示快捷键?