Zend_Dom-Query.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.dom.query">
  4. <title>Zend_Dom_Query</title>
  5. <para>
  6. <classname>Zend_Dom_Query</classname> provides mechanisms for querying
  7. <acronym>XML</acronym> and (X)HTML documents utilizing either XPath or
  8. <acronym>CSS</acronym> selectors. It was developed to aid with functional testing of
  9. <acronym>MVC</acronym> applications, but could also be used for rapid development of screen
  10. scrapers.
  11. </para>
  12. <para>
  13. <acronym>CSS</acronym> selector notation is provided as a simpler and more familiar
  14. notation for web developers to utilize when querying documents with <acronym>XML</acronym>
  15. structures. The notation should be familiar to anybody who has developed
  16. Cascading Style Sheets or who utilizes Javascript toolkits that provide
  17. functionality for selecting nodes utilizing <acronym>CSS</acronym> selectors
  18. (<ulink url="http://prototypejs.org/api/utility/dollar-dollar">Prototype's
  19. $$()</ulink> and
  20. <ulink url="http://api.dojotoolkit.org/jsdoc/dojo/HEAD/dojo.query">Dojo's
  21. dojo.query</ulink> were both inspirations for the component).
  22. </para>
  23. <sect2 id="zend.dom.query.operation">
  24. <title>Theory of Operation</title>
  25. <para>
  26. To use <classname>Zend_Dom_Query</classname>, you instantiate a
  27. <classname>Zend_Dom_Query</classname> object, optionally passing a document to
  28. query (a string). Once you have a document, you can use either the
  29. <methodname>query()</methodname> or <methodname>queryXpath()</methodname> methods; each
  30. method will return a <classname>Zend_Dom_Query_Result</classname> object with
  31. any matching nodes.
  32. </para>
  33. <para>
  34. The primary difference between <classname>Zend_Dom_Query</classname> and using
  35. DOMDocument + DOMXPath is the ability to select against <acronym>CSS</acronym>
  36. selectors. You can utilize any of the following, in any combination:
  37. </para>
  38. <itemizedlist>
  39. <listitem><para>
  40. <emphasis>element types</emphasis>: provide an element type to
  41. match: 'div', 'a', 'span', 'h2', etc.
  42. </para></listitem>
  43. <listitem><para>
  44. <emphasis>style attributes</emphasis>: <acronym>CSS</acronym> style attributes to
  45. match: '.error', 'div.error', 'label.required', etc. If an
  46. element defines more than one style, this will match as long as
  47. the named style is present anywhere in the style declaration.
  48. </para></listitem>
  49. <listitem><para>
  50. <emphasis>id attributes</emphasis>: element ID attributes to
  51. match: '#content', 'div#nav', etc.
  52. </para></listitem>
  53. <listitem>
  54. <para>
  55. <emphasis>arbitrary attributes</emphasis>: arbitrary element
  56. attributes to match. Three different types of matching are
  57. provided:
  58. </para>
  59. <itemizedlist>
  60. <listitem><para>
  61. <emphasis>exact match</emphasis>: the attribute exactly
  62. matches the string: 'div[bar="baz"]' would match a div
  63. element with a "bar" attribute that exactly matches the
  64. value "baz".
  65. </para></listitem>
  66. <listitem><para>
  67. <emphasis>word match</emphasis>: the attribute contains
  68. a word matching the string: 'div[bar~="baz"]' would match a div
  69. element with a "bar" attribute that contains the
  70. word "baz". '&lt;div bar="foo baz"&gt;' would match, but '&lt;div
  71. bar="foo bazbat"&gt;' would not.
  72. </para></listitem>
  73. <listitem><para>
  74. <emphasis>substring match</emphasis>: the attribute contains
  75. the string: 'div[bar*="baz"]' would match a div
  76. element with a "bar" attribute that contains the
  77. string "baz" anywhere within it.
  78. </para></listitem>
  79. </itemizedlist>
  80. </listitem>
  81. <listitem><para>
  82. <emphasis>direct descendents</emphasis>: utilize '&gt;' between
  83. selectors to denote direct descendents. 'div > span' would
  84. select only 'span' elements that are direct descendents of a
  85. 'div'. Can also be used with any of the selectors above.
  86. </para></listitem>
  87. <listitem>
  88. <para>
  89. <emphasis>descendents</emphasis>: string together
  90. multiple selectors to indicate a hierarchy along which
  91. to search. 'div .foo span #one' would select an element
  92. of id 'one' that is a descendent of arbitrary depth
  93. beneath a 'span' element, which is in turn a descendent
  94. of arbitrary depth beneath an element with a class of
  95. 'foo', that is an descendent of arbitrary depth beneath
  96. a 'div' element. For example, it would match the link to
  97. the word 'One' in the listing below:
  98. </para>
  99. <programlisting language="html"><![CDATA[
  100. <div>
  101. <table>
  102. <tr>
  103. <td class="foo">
  104. <div>
  105. Lorem ipsum <span class="bar">
  106. <a href="/foo/bar" id="one">One</a>
  107. <a href="/foo/baz" id="two">Two</a>
  108. <a href="/foo/bat" id="three">Three</a>
  109. <a href="/foo/bla" id="four">Four</a>
  110. </span>
  111. </div>
  112. </td>
  113. </tr>
  114. </table>
  115. </div>
  116. ]]></programlisting>
  117. </listitem>
  118. </itemizedlist>
  119. <para>
  120. Once you've performed your query, you can then work with the result
  121. object to determine information about the nodes, as well as to pull
  122. them and/or their content directly for examination and manipulation.
  123. <classname>Zend_Dom_Query_Result</classname> implements <code>Countable</code>
  124. and <code>Iterator</code>, and store the results internally as
  125. DOMNodes/DOMElements. As an example, consider the following call,
  126. that selects against the HTML above:
  127. </para>
  128. <programlisting language="php"><![CDATA[
  129. $dom = new Zend_Dom_Query($html);
  130. $results = $dom->query('.foo .bar a');
  131. $count = count($results); // get number of matches: 4
  132. foreach ($results as $result) {
  133. // $result is a DOMElement
  134. }
  135. ]]></programlisting>
  136. <para>
  137. <classname>Zend_Dom_Query</classname> also allows straight XPath queries
  138. utilizing the <methodname>queryXpath()</methodname> method; you can pass any
  139. valid XPath query to this method, and it will return a
  140. <classname>Zend_Dom_Query_Result</classname> object.
  141. </para>
  142. </sect2>
  143. <sect2 id="zend.dom.query.methods">
  144. <title>Methods Available</title>
  145. <para>
  146. The <classname>Zend_Dom_Query</classname> family of classes have the following
  147. methods available.
  148. </para>
  149. <sect3 id="zend.dom.query.methods.zenddomquery">
  150. <title>Zend_Dom_Query</title>
  151. <para>
  152. The following methods are available to
  153. <classname>Zend_Dom_Query</classname>:
  154. </para>
  155. <itemizedlist>
  156. <listitem><para>
  157. <methodname>setDocumentXml($document)</methodname>: specify an
  158. <acronym>XML</acronym> string to query against.
  159. </para></listitem>
  160. <listitem><para>
  161. <methodname>setDocumentXhtml($document)</methodname>: specify an
  162. <acronym>XHTML</acronym> string to query against.
  163. </para></listitem>
  164. <listitem><para>
  165. <methodname>setDocumentHtml($document)</methodname>: specify an HTML
  166. string to query against.
  167. </para></listitem>
  168. <listitem><para>
  169. <methodname>setDocument($document)</methodname>: specify a
  170. string to query against; <classname>Zend_Dom_Query</classname> will
  171. then attempt to autodetect the document type.
  172. </para></listitem>
  173. <listitem><para>
  174. <methodname>getDocument()</methodname>: retrieve the original document
  175. string provided to the object.
  176. </para></listitem>
  177. <listitem><para>
  178. <methodname>getDocumentType()</methodname>: retrieve the document
  179. type of the document provided to the object; will be one of
  180. the <constant>DOC_XML</constant>, <constant>DOC_XHTML</constant>, or
  181. <constant>DOC_HTML</constant> class constants.
  182. </para></listitem>
  183. <listitem><para>
  184. <methodname>query($query)</methodname>: query the document using
  185. <acronym>CSS</acronym> selector notation.
  186. </para></listitem>
  187. <listitem><para>
  188. <methodname>queryXpath($xPathQuery)</methodname>: query the document
  189. using XPath notation.
  190. </para></listitem>
  191. </itemizedlist>
  192. </sect3>
  193. <sect3 id="zend.dom.query.methods.zenddomqueryresult">
  194. <title>Zend_Dom_Query_Result</title>
  195. <para>
  196. As mentioned previously, <classname>Zend_Dom_Query_Result</classname>
  197. implements both <code>Iterator</code> and
  198. <code>Countable</code>, and as such can be used in a
  199. <code>foreach</code> loop as well as with the
  200. <methodname>count()</methodname> function. Additionally, it exposes the
  201. following methods:
  202. </para>
  203. <itemizedlist>
  204. <listitem><para>
  205. <methodname>getCssQuery()</methodname>: return the <acronym>CSS</acronym>
  206. selector query used to produce the result (if any).
  207. </para></listitem>
  208. <listitem><para>
  209. <methodname>getXpathQuery()</methodname>: return the XPath query
  210. used to produce the result. Internally,
  211. <classname>Zend_Dom_Query</classname> converts <acronym>CSS</acronym> selector
  212. queries to XPath, so this value will always be populated.
  213. </para></listitem>
  214. <listitem><para>
  215. <methodname>getDocument()</methodname>: retrieve the DOMDocument the
  216. selection was made against.
  217. </para></listitem>
  218. </itemizedlist>
  219. </sect3>
  220. </sect2>
  221. </sect1>
  222. <!--
  223. vim:se ts=4 sw=4 et:
  224. -->