Exforsys.com
 
Home Tutorials PHP Oracle
 

Querying a DOM Document with XPath

 

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.


Sample Code
  1. <?php
  2.         //File: XPath.php
  3.         $dom = new DomDocument();
  4.         $dom->load('employees.xml');
  5.         $xpath = new DOMXPath($dom);
  6.         $query = '//EMPLOYEE/SALARY[. > "15000"]';
  7.         $emps = $xpath->query($query);
  8.         print '<font face="Arial">';
  9.         print '<h3>Executive officers whose salaries > $15,000</h3>';
  10.         print '<table border="1" cellpadding="5"><p>';
  11.         print '<th>Employee ID</th><th>Last Name</th><th>Salary</th><p>';
  12.         foreach ($emps as $emp) {
  13.                 print '<tr><td>'.$emp->parentNode->getAttribute('id').'</td><p>';
  14.                 print '<td>'.$emp->previousSibling->nodeValue.'</td><p>';
  15.                 print '<td>'.$emp->nodeValue.'</td></tr><p>';
  16.         }
  17.         print '</table>';
  18.         print '</font>';
  19. ?>
Copyright exforsys.com


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" target="_blank" rel="nofollow".



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.



Read Next: Transforming and Processing XML with XSLT



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape