Location: PHPKode > projects > Open Power Template > docs/Opt/api.opt-xml-scannable.get-elements-ext.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="pl">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<meta name="robots" content="all" />

	<title>getElementsExt() - Open Power Template</title>
	
	<link rel="stylesheet" type="text/css" href="design/generic.css" media="all"  />
	<link rel="stylesheet" type="text/css" href="design/print.css" media="print" />
	<!--[if lte IE 6]><link rel="stylesheet" href="design/ie.css" type="text/css" /><![endif]-->	
	<!--[if IE 7]><link rel="stylesheet" href="design/ie7.css" type="text/css" /><![endif]-->
</head>
<body>

<div id="wrap">
	<div id="header">
		<h1>Open Power Template 2.0</h1>
		<h2>getElementsExt()</h2>
		<p class="generated">@ 02.09.2010</p>
		<p class="location"><a href="index.html"><strong>User manual</strong></a> &raquo; <a href="api.html">API Reference</a> &raquo; <a href="api.opt-xml-scannable.html">Opt_Xml_Scannable</a> &raquo; <a href="api.opt-xml-scannable.get-elements-ext.html">Opt_Xml_Scannable::getElementsExt()</a></p>
	</div>
	
	<div id="content"><dl class="location"><dt><a href="api.opt-xml-scannable.html">7.18. Opt_Xml_Scannable class</a><br/>7.18.8. getElementsExt()</dt><dd class="prev">7.18.7. getElementsByTagNameNS()<br/><a href="api.opt-xml-scannable.get-elements-by-tag-name-ns.html">&laquo; Previous</a></dd><dd class="next">7.18.9. getLastChild()<br/><a href="api.opt-xml-scannable.get-last-child.html">Next &raquo;</a></dd></dl>	<h1>7.18.8. getElementsExt()</h1><div class="tf_reference"><table><tr><th>Status</th><td>public</td></tr><tr><th>Reference</th><td><code>array getElementsExt(string $namespace, string $name)</code></td></tr><tr><th>Versions</th><td>since <code>2.0-dev8</code></td></tr>
</table><hr/></div><p>This method works much like <a href="api.opt-xml-scannable.get-elements-by-tag-name-ns.html" title="7.18.7. Opt_Xml_Scannable::getElementsByTagNameNS()">Opt_Xml_Scannable::getElementsByTagNameNS()</a>, however it does not check the descendants of matching nodes. Its use is very common in OPT. Suppose we are writing the new instruction processor. The main instruction tag is <code>opt:foo</code>, but it must have also a descendant <code>opt:bar</code>:</p>

<pre class="xml"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;opt:foo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;opt:bar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Hello world!<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/opt:bar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/opt:foo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre>

<p>While parsing <code>opt:foo</code> we need to find the <code>opt:bar</code> for some reason and moreover simply to check if it is defined exactly once:</p>

<pre class="php"><span style="color: #000088;">$barNodes</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagNameNS</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'opt'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'bar'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/sizeof"><span style="color: #990000;">sizeof</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$nodes</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> 1<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    throw <span style="color: #000000; font-weight: bold;">new</span> Opt_InstructionTooManyItems_Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'opt:bar'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'opt:foo'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'One'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>

<p>The first impression is that this code is correct. However, let's take a look at the following situation:</p>

<pre class="xml"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;opt:foo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;opt:bar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;opt:foo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;opt:bar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Hello world!<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/opt:bar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/opt:foo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/opt:bar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/opt:foo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre>

<p>In this case the code above will surely cause error, even if it is easily noticeable that the second <code>opt:bar</code> should be connected to the nested <code>opt:foo</code>! Here, we must use <code>getElementsExt()</code>:</p>

<pre class="php"><span style="color: #000088;">$barNodes</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsExt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'opt'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'bar'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/sizeof"><span style="color: #990000;">sizeof</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$nodes</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> 1<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    throw <span style="color: #000000; font-weight: bold;">new</span> Opt_InstructionTooManyItems_Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'opt:bar'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'opt:foo'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'One'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>

<p>As this method does not visit the descendants of <code>opt:bar</code>, everything starts to work.</p>
<h4>See also:</h4><ul><li><a href="api.opt-xml-scannable.get-elements-by-tag-name.html">7.18.6. Opt_Xml_Scannable::getElementsByTagName()</a></li><li><a href="api.opt-xml-scannable.get-elements-by-tag-name-ns.html">7.18.7. Opt_Xml_Scannable::getElementsByTagNameNS()</a></li></ul><dl class="location location-bottom"><dt>7.18.8. getElementsExt()<br/><a href="api.opt-xml-scannable.html">7.18. Opt_Xml_Scannable class</a></dt><dd class="prev"><a href="api.opt-xml-scannable.get-elements-by-tag-name-ns.html">&laquo; Previous</a><br/>7.18.7. getElementsByTagNameNS()</dd><dd class="next"><a href="api.opt-xml-scannable.get-last-child.html">Next &raquo;</a><br/>7.18.9. getLastChild()</dd></dl>		</div>
	
	<div id="footer">
		<p>Copyright &copy; <a href="http://www.invenzzia.org/">Invenzzia Group 2008-2009</a></p>
		<p>Available under the terms of license: <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License 1.2</a></p>
		<p>Generated by <strong>TypeFriendly 0.1.4</strong> by <a href="http://www.invenzzia.org/">Invenzzia</a></p>
	</div>
</div>

</body>
</html>
Return current item: Open Power Template