Zend_Dom-Query.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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>
  40. <para>
  41. <emphasis>element types</emphasis>: provide an element type to
  42. match: 'div', 'a', 'span', 'h2', etc.
  43. </para>
  44. </listitem>
  45. <listitem>
  46. <para>
  47. <emphasis>style attributes</emphasis>: <acronym>CSS</acronym> style attributes
  48. to match: '.error', 'div.error', 'label.required', etc. If an
  49. element defines more than one style, this will match as long as
  50. the named style is present anywhere in the style declaration.
  51. </para>
  52. </listitem>
  53. <listitem>
  54. <para>
  55. <emphasis>id attributes</emphasis>: element ID attributes to
  56. match: '#content', 'div#nav', etc.
  57. </para>
  58. </listitem>
  59. <listitem>
  60. <para>
  61. <emphasis>arbitrary attributes</emphasis>: arbitrary element
  62. attributes to match. Three different types of matching are
  63. provided:
  64. </para>
  65. <itemizedlist>
  66. <listitem>
  67. <para>
  68. <emphasis>exact match</emphasis>: the attribute exactly
  69. matches the string: 'div[bar="baz"]' would match a div
  70. element with a "bar" attribute that exactly matches the
  71. value "baz".
  72. </para>
  73. </listitem>
  74. <listitem>
  75. <para>
  76. <emphasis>word match</emphasis>: the attribute contains
  77. a word matching the string: 'div[bar~="baz"]' would match a div
  78. element with a "bar" attribute that contains the
  79. word "baz". '&lt;div bar="foo baz"&gt;' would match, but '&lt;div
  80. bar="foo bazbat"&gt;' would not.
  81. </para>
  82. </listitem>
  83. <listitem>
  84. <para>
  85. <emphasis>substring match</emphasis>: the attribute contains
  86. the string: 'div[bar*="baz"]' would match a div
  87. element with a "bar" attribute that contains the
  88. string "baz" anywhere within it.
  89. </para>
  90. </listitem>
  91. </itemizedlist>
  92. </listitem>
  93. <listitem>
  94. <para>
  95. <emphasis>direct descendents</emphasis>: utilize '&gt;' between
  96. selectors to denote direct descendents. 'div > span' would
  97. select only 'span' elements that are direct descendents of a
  98. 'div'. Can also be used with any of the selectors above.
  99. </para>
  100. </listitem>
  101. <listitem>
  102. <para>
  103. <emphasis>descendents</emphasis>: string together
  104. multiple selectors to indicate a hierarchy along which
  105. to search. 'div .foo span #one' would select an element
  106. of id 'one' that is a descendent of arbitrary depth
  107. beneath a 'span' element, which is in turn a descendent
  108. of arbitrary depth beneath an element with a class of
  109. 'foo', that is an descendent of arbitrary depth beneath
  110. a 'div' element. For example, it would match the link to
  111. the word 'One' in the listing below:
  112. </para>
  113. <programlisting language="html"><![CDATA[
  114. <div>
  115. <table>
  116. <tr>
  117. <td class="foo">
  118. <div>
  119. Lorem ipsum <span class="bar">
  120. <a href="/foo/bar" id="one">One</a>
  121. <a href="/foo/baz" id="two">Two</a>
  122. <a href="/foo/bat" id="three">Three</a>
  123. <a href="/foo/bla" id="four">Four</a>
  124. </span>
  125. </div>
  126. </td>
  127. </tr>
  128. </table>
  129. </div>
  130. ]]></programlisting>
  131. </listitem>
  132. </itemizedlist>
  133. <para>
  134. Once you've performed your query, you can then work with the result
  135. object to determine information about the nodes, as well as to pull
  136. them and/or their content directly for examination and manipulation.
  137. <classname>Zend_Dom_Query_Result</classname> implements <code>Countable</code>
  138. and <code>Iterator</code>, and store the results internally as
  139. DOMNodes/DOMElements. As an example, consider the following call,
  140. that selects against the HTML above:
  141. </para>
  142. <programlisting language="php"><![CDATA[
  143. $dom = new Zend_Dom_Query($html);
  144. $results = $dom->query('.foo .bar a');
  145. $count = count($results); // get number of matches: 4
  146. foreach ($results as $result) {
  147. // $result is a DOMElement
  148. }
  149. ]]></programlisting>
  150. <para>
  151. <classname>Zend_Dom_Query</classname> also allows straight XPath queries
  152. utilizing the <methodname>queryXpath()</methodname> method; you can pass any
  153. valid XPath query to this method, and it will return a
  154. <classname>Zend_Dom_Query_Result</classname> object.
  155. </para>
  156. </sect2>
  157. <sect2 id="zend.dom.query.methods">
  158. <title>Methods Available</title>
  159. <para>
  160. The <classname>Zend_Dom_Query</classname> family of classes have the following
  161. methods available.
  162. </para>
  163. <sect3 id="zend.dom.query.methods.zenddomquery">
  164. <title>Zend_Dom_Query</title>
  165. <para>
  166. The following methods are available to
  167. <classname>Zend_Dom_Query</classname>:
  168. </para>
  169. <itemizedlist>
  170. <listitem>
  171. <para>
  172. <methodname>setDocumentXml($document)</methodname>: specify an
  173. <acronym>XML</acronym> string to query against.
  174. </para>
  175. </listitem>
  176. <listitem>
  177. <para>
  178. <methodname>setDocumentXhtml($document)</methodname>: specify an
  179. <acronym>XHTML</acronym> string to query against.
  180. </para>
  181. </listitem>
  182. <listitem>
  183. <para>
  184. <methodname>setDocumentHtml($document)</methodname>: specify an HTML
  185. string to query against.
  186. </para>
  187. </listitem>
  188. <listitem>
  189. <para>
  190. <methodname>setDocument($document)</methodname>: specify a
  191. string to query against; <classname>Zend_Dom_Query</classname> will
  192. then attempt to autodetect the document type.
  193. </para>
  194. </listitem>
  195. <listitem>
  196. <para>
  197. <methodname>getDocument()</methodname>: retrieve the original document
  198. string provided to the object.
  199. </para>
  200. </listitem>
  201. <listitem>
  202. <para>
  203. <methodname>getDocumentType()</methodname>: retrieve the document
  204. type of the document provided to the object; will be one of
  205. the <constant>DOC_XML</constant>, <constant>DOC_XHTML</constant>, or
  206. <constant>DOC_HTML</constant> class constants.
  207. </para>
  208. </listitem>
  209. <listitem>
  210. <para>
  211. <methodname>query($query)</methodname>: query the document using
  212. <acronym>CSS</acronym> selector notation.
  213. </para>
  214. </listitem>
  215. <listitem>
  216. <para>
  217. <methodname>queryXpath($xPathQuery)</methodname>: query the document
  218. using XPath notation.
  219. </para>
  220. </listitem>
  221. </itemizedlist>
  222. </sect3>
  223. <sect3 id="zend.dom.query.methods.zenddomqueryresult">
  224. <title>Zend_Dom_Query_Result</title>
  225. <para>
  226. As mentioned previously, <classname>Zend_Dom_Query_Result</classname>
  227. implements both <code>Iterator</code> and
  228. <code>Countable</code>, and as such can be used in a
  229. <code>foreach</code> loop as well as with the
  230. <methodname>count()</methodname> function. Additionally, it exposes the
  231. following methods:
  232. </para>
  233. <itemizedlist>
  234. <listitem>
  235. <para>
  236. <methodname>getCssQuery()</methodname>: return the <acronym>CSS</acronym>
  237. selector query used to produce the result (if any).
  238. </para>
  239. </listitem>
  240. <listitem>
  241. <para>
  242. <methodname>getXpathQuery()</methodname>: return the XPath query
  243. used to produce the result. Internally,
  244. <classname>Zend_Dom_Query</classname> converts <acronym>CSS</acronym>
  245. selector queries to XPath, so this value will always be populated.
  246. </para>
  247. </listitem>
  248. <listitem>
  249. <para>
  250. <methodname>getDocument()</methodname>: retrieve the DOMDocument the
  251. selection was made against.
  252. </para>
  253. </listitem>
  254. </itemizedlist>
  255. </sect3>
  256. </sect2>
  257. </sect1>
  258. <!--
  259. vim:se ts=4 sw=4 et:
  260. -->