The XPath support was introduced to provide an access to the result set after doing the SQL query. This allows further proccessing of the result set without quering the database again.
XPath is a W3-Standard and for further information about that, ask your preferred XSL/XML book, or http://www.w3.org/.
Passing an XPath expression
XML_sql2xml provides two functions for querying the result set: getXpathValue() and getXpathChildValues(). Both expect a XPath query and return the result as string or array.
<?php include_once("XML/sql2xml.php"); $sql2xmlclass = new xml_sql2xml("mysql://username:password@localhost/xmltest"); $sql2xmlclass->add("select * from bands"); $xmlstring = $sql2xmlclass->getXpathChildValues("/root/result/row[id = '2']"); ?>
$xmlstring contains:
Array ( [] => [id] => 2 [name] => Only Stupids [birth_year] => 1997 [birth_place] => New York [genre] => Hip Hop )
Mixing SQL and XPath query
You can insert an XPath query into an SQL query. In the example, first a SQL query is done, in the second a SQL query done again - but the parameter for bandsID is taken from the XPath expression in the curly braces. This expression is processed on the result set of the first SQL query.
<?php include_once("XML/sql2xml.php"); $sql2xmlclass = new xml_sql2xml("mysql://username:password@localhost/xmltest"); $sql2xmlclass->add("select * from bands"); $sql2xmlclass->add("select * from albums where bandsID = {/root/result/row[name = 'The Blabbers']/id}"); $xmlstring = $sql2xmlclass->getxml(); ?>
Deliver First Class Web Sites: 101 Essential Checklists Want to learn how to make your web sites usable and accessible? Want to ensure that your sites meet current best practice, without spending hours trawling through incomprehensible specifications and recommendations from dozens of different books, research papers, and web sites? Want to make sure that the sites you build are "right the first time," requiring no costly redevelopments?