Sponsored Links
PHP Oracle Tutorials
- PHP Oracle Web Development
- XML Processing in PHP and Oracle Applications
- Creating XML with the DOM PHP Extension
- Querying a DOM Document with XPath
- Transforming and Processing XML with XSLT
- Performing XML Processing inside the Database
- Moving All the XML Processing into the Database
- Performing XSLT Transformations inside the Database
- Using Oracle Database for Storing, Modifying, and Retrieving XML Data
- Using XMLType for Handling XML Data in the Database
- Using XML Schemas
- Retrieving XML Data
- Using XMLType Views
- Performing DML Operations on XML Schema Based XMLType Views
- Using Oracle XML DB Repository
- Accessing Repository Resources with SQL
- Taking Advantage of Standard Internet Protocols
- Querying Data with Oracle XQuery
- Breaking up XML into Relational Data
Tutorials
PHP OracleQuerying 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.
- <?php
- //File: XPath.php
- $dom = new DomDocument();
- $dom->load('employees.xml');
- $xpath = new DOMXPath($dom);
- $query = '//EMPLOYEE/SALARY[. > "15000"]';
- $emps = $xpath->query($query);
- foreach ($emps as $emp) {
- }
- ?>
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.
Comments
Sponsored Links
