2010-03-28 22:10:10 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
2009-07-11 06:52:11 +00:00
|
|
|
<!-- $Revision$ -->
|
2007-12-16 13:18:08 +00:00
|
|
|
<refentry xml:id="domxpath.query" xmlns="http://docbook.org/ns/docbook">
|
2004-12-20 15:57:49 +00:00
|
|
|
<refnamediv>
|
2007-12-16 13:18:08 +00:00
|
|
|
<refname>DOMXPath::query</refname>
|
2004-12-20 15:57:49 +00:00
|
|
|
<refpurpose>
|
|
|
|
Evaluates the given XPath expression
|
|
|
|
</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
<refsect1 role="description">
|
|
|
|
&reftitle.description;
|
2007-12-16 13:18:08 +00:00
|
|
|
<methodsynopsis>
|
2021-01-01 19:46:14 +00:00
|
|
|
<modifier>public</modifier> <type>mixed</type><methodname>DOMXPath::query</methodname>
|
2007-12-16 13:18:08 +00:00
|
|
|
<methodparam><type>string</type><parameter>expression</parameter></methodparam>
|
2021-01-01 19:46:14 +00:00
|
|
|
<methodparam choice="opt"><type class="union"><type>DOMNode</type><type>null</type></type><parameter>contextNode</parameter><initializer>&null;</initializer></methodparam>
|
2018-01-25 19:11:34 +00:00
|
|
|
<methodparam choice="opt"><type>bool</type><parameter>registerNodeNS</parameter><initializer>&true;</initializer></methodparam>
|
2007-12-16 13:18:08 +00:00
|
|
|
</methodsynopsis>
|
2004-12-20 15:57:49 +00:00
|
|
|
<para>
|
2004-12-27 13:13:26 +00:00
|
|
|
Executes the given XPath <parameter>expression</parameter>.
|
2004-12-20 15:57:49 +00:00
|
|
|
</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>
|
2021-01-01 19:46:14 +00:00
|
|
|
<term><parameter>contextNode</parameter></term>
|
2004-12-20 15:57:49 +00:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2021-01-01 19:46:14 +00:00
|
|
|
The optional <parameter>contextNode</parameter> can be specified for
|
2004-12-20 15:57:49 +00:00
|
|
|
doing relative XPath queries. By default, the queries are relative to
|
|
|
|
the root element.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2010-10-23 05:30:55 +00:00
|
|
|
<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>
|
2004-12-20 15:57:49 +00:00
|
|
|
</variablelist>
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
<refsect1 role="returnvalues">
|
|
|
|
&reftitle.returnvalues;
|
|
|
|
<para>
|
2010-12-07 09:03:56 +00:00
|
|
|
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
|
2021-01-01 19:46:14 +00:00
|
|
|
<parameter>contextNode</parameter> is invalid,
|
2010-12-07 09:03:56 +00:00
|
|
|
<methodname>DOMXPath::query</methodname> returns &false;.
|
2004-12-20 15:57:49 +00:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
<refsect1 role="examples">
|
|
|
|
&reftitle.examples;
|
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title>Getting all the english books</title>
|
|
|
|
<programlisting role="php">
|
2004-12-19 13:21:33 +00:00
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
|
|
|
|
$doc = new DOMDocument;
|
2004-12-19 22:16:44 +00:00
|
|
|
|
|
|
|
// We don't want to bother with white spaces
|
|
|
|
$doc->preserveWhiteSpace = false;
|
|
|
|
|
2018-01-30 13:47:20 +00:00
|
|
|
$doc->load('book.xml');
|
2004-12-19 13:21:33 +00:00
|
|
|
|
|
|
|
$xpath = new DOMXPath($doc);
|
|
|
|
|
2020-03-06 00:09:07 +00:00
|
|
|
// We start from the root element
|
2004-12-19 22:16:44 +00:00
|
|
|
$query = '//book/chapter/para/informaltable/tgroup/tbody/row/entry[. = "en"]';
|
2004-12-19 13:21:33 +00:00
|
|
|
|
|
|
|
$entries = $xpath->query($query);
|
|
|
|
|
|
|
|
foreach ($entries as $entry) {
|
2004-12-19 22:16:44 +00:00
|
|
|
echo "Found {$entry->previousSibling->previousSibling->nodeValue}," .
|
|
|
|
" by {$entry->previousSibling->nodeValue}\n";
|
2004-12-19 13:21:33 +00:00
|
|
|
}
|
|
|
|
?>
|
|
|
|
]]>
|
2004-12-20 15:57:49 +00:00
|
|
|
</programlisting>
|
|
|
|
&example.outputs;
|
|
|
|
<screen>
|
2004-12-19 13:21:33 +00:00
|
|
|
<![CDATA[
|
2004-12-19 22:16:44 +00:00
|
|
|
Found The Grapes of Wrath, by John Steinbeck
|
|
|
|
Found The Pearl, by John Steinbeck
|
2004-12-19 13:21:33 +00:00
|
|
|
]]>
|
2004-12-20 15:57:49 +00:00
|
|
|
</screen>
|
2004-05-09 15:07:48 +00:00
|
|
|
<para>
|
2021-01-01 19:46:14 +00:00
|
|
|
We can also use the <parameter>contextNode</parameter> parameter to shorten
|
2004-12-20 15:57:49 +00:00
|
|
|
our expression:
|
2004-05-09 15:07:48 +00:00
|
|
|
</para>
|
2004-12-20 15:57:49 +00:00
|
|
|
<programlisting role="php">
|
2004-12-19 13:21:33 +00:00
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
|
|
|
|
$doc = new DOMDocument;
|
2004-12-19 22:16:44 +00:00
|
|
|
$doc->preserveWhiteSpace = false;
|
|
|
|
|
2004-12-27 13:13:26 +00:00
|
|
|
$doc->load('book.xml');
|
2004-12-19 13:21:33 +00:00
|
|
|
|
|
|
|
$xpath = new DOMXPath($doc);
|
|
|
|
|
|
|
|
$tbody = $doc->getElementsByTagName('tbody')->item(0);
|
|
|
|
|
|
|
|
// our query is relative to the tbody node
|
2004-12-19 22:16:44 +00:00
|
|
|
$query = 'row/entry[. = "en"]';
|
2004-12-19 13:21:33 +00:00
|
|
|
|
|
|
|
$entries = $xpath->query($query, $tbody);
|
|
|
|
|
|
|
|
foreach ($entries as $entry) {
|
2004-12-19 22:16:44 +00:00
|
|
|
echo "Found {$entry->previousSibling->previousSibling->nodeValue}," .
|
|
|
|
" by {$entry->previousSibling->nodeValue}\n";
|
2004-12-19 13:21:33 +00:00
|
|
|
}
|
|
|
|
?>
|
|
|
|
]]>
|
2004-12-20 15:57:49 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
2004-12-27 13:13:26 +00:00
|
|
|
<refsect1 role="seealso">
|
|
|
|
&reftitle.seealso;
|
|
|
|
<para>
|
|
|
|
<simplelist>
|
2009-06-08 21:01:23 +00:00
|
|
|
<member><methodname>DOMXPath::evaluate</methodname></member>
|
2004-12-27 13:13:26 +00:00
|
|
|
</simplelist>
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
2004-12-20 15:57:49 +00:00
|
|
|
</refentry>
|
2004-05-09 15:07:48 +00:00
|
|
|
<!-- 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
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2004-05-09 15:07:48 +00:00
|
|
|
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
|
|
|
|
-->
|