Querying a DOM Document with XPath
One way to access the DOM tree in a DOMDocument object is through an associated DOMXPath object. Identifying a specifi c node or nodes within the DOM tree of a DOMDocument object with this approach involves use of appropriate XPath expressions passed to the DOMXPath object as parameters.
NOTE: While the example in this section shows how XPath can be used in PHP, Oracle also has some SQL functions operating on XML, such as existsNode, extractValue, and updateXML, which take XPathexpression arguments.
The following script illustrates how to access XML content held in a DOMDocument object through the DOMXPath object associated with that DOMDocument.
<?php //File: XPath.php $dom = new DomDocument(); $dom->load('employees.xml'); $xpath = new DOMXPath($dom); $query = '//EMPLOYEE/SALARY[. > "15000"]'; $emps = $xpath->query($query); print '<font face="Arial">'; print '<h3>Executive officers whose salaries > $15,000</h3>'; print '<table border="1" cellpadding="5"><pre>'; print '<th>Employee ID</th><th>Last Name</th><th>Salary</th><pre>'; foreach ($emps as $emp) { print '<tr><td>'.$emp->parentNode->getAttribute('id').'</td><pre>'; print '<td>'.$emp->previousSibling->nodeValue.'</td><pre>'; print '<td>'.$emp->nodeValue.'</td></tr><pre>'; } print '</table>'; print '</font>'; ?>
%3C%3Fphp%0D%0A%26nbsp%3B%20%26nbsp%3B%2F%2FFile%3A%20XPath.php%0D%0A%26nbsp%3B%20%26nbsp%3B%24dom%20%3D%20new%20DomDocument%28%29%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%24dom-%3Eload%28%27employees.xml%27%29%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%24xpath%20%3D%20new%20DOMXPath%28%24dom%29%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%24query%20%3D%20%27%2F%2FEMPLOYEE%2FSALARY%5B.%20%3E%20%2215000%22%5D%27%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%24emps%20%3D%20%24xpath-%3Equery%28%24query%29%3B%0D%0A%26nbsp%3B%20%26nbsp%3Bprint%20%27%3Cfont%20face%3D%22Arial%22%3E%27%3B%0D%0A%26nbsp%3B%20%26nbsp%3Bprint%20%27%3Ch3%3EExecutive%20officers%20whose%20salaries%20%3E%20%2415%2C000%3C%2Fh3%3E%27%3B%0D%0A%26nbsp%3B%20%26nbsp%3Bprint%20%27%3Ctable%20border%3D%221%22%20cellpadding%3D%225%22%3E%3Cpre%3E%27%3B%0D%0A%26nbsp%3B%20%26nbsp%3Bprint%20%27%3Cth%3EEmployee%20ID%3C%2Fth%3E%3Cth%3ELast%20Name%3C%2Fth%3E%3Cth%3ESalary%3C%2Fth%3E%3Cpre%3E%27%3B%0D%0A%26nbsp%3B%20%26nbsp%3Bforeach%20%28%24emps%20as%20%24emp%29%20%7B%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3Bprint%20%27%3Ctr%3E%3Ctd%3E%27.%24emp-%3EparentNode-%3EgetAttribute%28%27id%27%29.%27%3C%2Ftd%3E%3Cpre%3E%27%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3Bprint%20%27%3Ctd%3E%27.%24emp-%3EpreviousSibling-%3EnodeValue.%27%3C%2Ftd%3E%3Cpre%3E%27%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3Bprint%20%27%3Ctd%3E%27.%24emp-%3EnodeValue.%27%3C%2Ftd%3E%3C%2Ftr%3E%3Cpre%3E%27%3B%0D%0A%26nbsp%3B%20%26nbsp%3B%7D%0D%0A%26nbsp%3B%20%26nbsp%3Bprint%20%27%3C%2Ftable%3E%27%3B%0D%0A%26nbsp%3B%20%26nbsp%3Bprint%20%27%3C%2Ffont%3E%27%3B%0D%0A%3F%3E
Unlike the preceding example where you generated an XML document from
scratch, here you load it from a fi le, using the load method of the DOMDocument object. After the document is loaded, you create a new DOMXPath object, and
associate it with the newly created DOMDocument object. The XPath expression used in the above script is to be applied to the employees XML document loaded to DOMDocument object. You use this expression to identify all the SALARY nodes whose values exceed 15000, passing it to the DOMXPath's query method as the parameter. NOTE: For more information on XPath, you can refer to the W3C XML Path Language (XPath) Version 1.0 recommendation at http://www.w3.org/TR/xpath.
To iterate over the result set returned by the query issued within the script, you use the foreach construct. Since each row of the result set represents a SALARY node defined within its parent EMPLOYEE node, you access that parent node using the parentNode method of the DOMNode object representing the SALARY node being processed. However, to access the corresponding ENAME node you use the previousSibling method of the DOMNode object. If you run the XPath.php script discussed here, your browser should display an HTML table representing the list of employees whose salaries exceed 15,000.
Trackback(0)
|