<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="domxpath.query" xmlns="http://docbook.org/ns/docbook">
 <refnamediv>
  <refname>DOMXPath::query</refname>
  <refpurpose>
   Evaluates the given XPath expression 
  </refpurpose>
 </refnamediv>
 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis role="DOMXPath">
   <modifier>public</modifier> <type>mixed</type><methodname>DOMXPath::query</methodname>
   <methodparam><type>string</type><parameter>expression</parameter></methodparam>
   <methodparam choice="opt"><type class="union"><type>DOMNode</type><type>null</type></type><parameter>contextNode</parameter><initializer>&null;</initializer></methodparam>
   <methodparam choice="opt"><type>bool</type><parameter>registerNodeNS</parameter><initializer>&true;</initializer></methodparam>
  </methodsynopsis>
  <para>
   Executes the given XPath <parameter>expression</parameter>.
  </para>
 </refsect1>
 <refsect1 role="parameters">
  &reftitle.parameters;
  <para>
   <variablelist>
    <varlistentry>
     <term><parameter>expression</parameter></term>
     <listitem>
      <para>
       The XPath expression to execute.
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>contextNode</parameter></term>
     <listitem>
      <para>
       The optional <parameter>contextNode</parameter> can be specified for
       doing relative XPath queries. By default, the queries are relative to 
       the root element.
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>registerNodeNS</parameter></term>
     <listitem>
      <para>
       The optional <parameter>registerNodeNS</parameter> can be specified to 
       disable automatic registration of the context node.
      </para>
     </listitem>
    </varlistentry>
   </variablelist>
  </para>
 </refsect1>
 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
  <para>
   Returns a <classname>DOMNodeList</classname> containing all nodes matching
   the given XPath <parameter>expression</parameter>. Any expression which
   does not return nodes will return an empty
   <classname>DOMNodeList</classname>.
  </para>
  <para>
   If the <parameter>expression</parameter> is malformed or the
   <parameter>contextNode</parameter> is invalid,
   <methodname>DOMXPath::query</methodname> returns &false;.
  </para>
 </refsect1>
 <refsect1 role="examples">
  &reftitle.examples;
  <para>
   <example>
    <title>Getting all the english books</title>
    <programlisting role="php">
<![CDATA[
<?php

$doc = new DOMDocument;

// We don't want to bother with white spaces
$doc->preserveWhiteSpace = false;

$doc->load('book.xml');

$xpath = new DOMXPath($doc);

// We start from the root element
$query = '//book/chapter/para/informaltable/tgroup/tbody/row/entry[. = "en"]';

$entries = $xpath->query($query);

foreach ($entries as $entry) {
    echo "Found {$entry->previousSibling->previousSibling->nodeValue}," .
         " by {$entry->previousSibling->nodeValue}\n";
}
?>
]]>
    </programlisting>
    &example.outputs;
    <screen>
<![CDATA[
Found The Grapes of Wrath, by John Steinbeck
Found The Pearl, by John Steinbeck
]]>
    </screen>
    <para>
     We can also use the <parameter>contextNode</parameter> parameter to shorten
     our expression:
    </para>
    <programlisting role="php">
<![CDATA[
<?php

$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;

$doc->load('book.xml');

$xpath = new DOMXPath($doc);

$tbody = $doc->getElementsByTagName('tbody')->item(0);

// our query is relative to the tbody node
$query = 'row/entry[. = "en"]';

$entries = $xpath->query($query, $tbody);

foreach ($entries as $entry) {
    echo "Found {$entry->previousSibling->previousSibling->nodeValue}," .
         " by {$entry->previousSibling->nodeValue}\n";
}
?>
]]>
    </programlisting>
   </example>
  </para>
 </refsect1>
 <refsect1 role="seealso">
  &reftitle.seealso;
  <para>
   <simplelist>
    <member><methodname>DOMXPath::evaluate</methodname></member>
   </simplelist>
  </para>
 </refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
