Introduction -- Using XPath
Why XPath support?
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.
Пример 63-1. getXpathValue <?php
include_once("XML/sql2xml.php");
$sql2xmlclass = new xml_sql2xml("mysql://username:password@localhost/xmltest");
$sql2xmlclass->add("select * from bands");
$xmlstring = $sql2xmlclass->getXpathValue("/root/result/row[id = '2']/name");
?> |
$xmlstring contains:
$xmlstring = 'Only Stupids' |
|
Пример 63-2. getXpathChildValues <?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.
Пример 63-3. Mixed 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();
?> |
$xmlstring = '
<?xml version="1.0"?>
<root>
<result>
<row>
<id>1</id>
<name>The Blabbers</name>
<birth_year>1998</birth_year>
<birth_place>London</birth_place>
<genre>Rock'n'Roll</genre>
</row>
<row>
<id>2</id>
<name>Only Stupids</name>
<birth_year>1997</birth_year>
<birth_place>New York</birth_place>
<genre>Hip Hop</genre>
</row>
</result>
<result>
<row>
<id>1</id>
<bandsID>1</bandsID>
<title>BlaBla</title>
<year>1998</year>
<comment>Their first one</comment>
</row>
<row>
<id>2</id>
<bandsID>1</bandsID>
<title>More Talks</title>
<year>2000</year>
<comment>The second one</comment>
</row>
</result>
</root> |
|
HIVE: All information for read only. Please respect copyright! |
|