J Sharman wrote:
I am fairly familiar with PHP and MYSQL and I know a little about XML.
I would like to know if it is possible to do queries on XML similar to the
way that you do queries on MYSQL and then use the results.
An example of a simple PHP/MYSQL setup.
<?php
$user="username";
$host="sqlserver";
$password="password";
$database="database";
$connection = mysql_connect($host,$user,$password)
or die ("Couldn't connect to the server");
$db = mysql_select_db($database,$connection)
or die ("Couldn't connect to the server");
$query = "SELECT * FROM database WHERE(Criteria)";
$result = mysql_query($query)
or die ("Couldn't execute query.");
while ($row = mysql_fetch_array($result))
{
extract($row);
echo"Set of instructions to carry out for each row in the
result";
}
?>
The question is, is it possible to do similar things with PHP/XML?
Lets say that I have an XML file located at a certain URL and I want to run
a query on it and use the results.
1. Is it possible?
YES. Any furthermore, you have choices.
2. What is the easiest way to do it?
Maybe you will want to decide this for yourself once checking out your
options. I'll list some down the bottom.
3. How would I translate the above code to work with XML?
//*[(criteria)]
Depending on what it is exactly you're interested in extracting and what
exactly the criteria is for extracting this, the complexity will go up.
That was an XPath query by the way.
4. Is ther resource on the web on how to structure the queries?
For XPath, do the tutorials at zvon.org
OK, stylesheet processors come with XPath built in, but you may not
neccesarily be after a full templating solution, you may just want some
data from the XML document to use in your code. And you seem to be
familiar with XSL so you know how you could use that.
The two main other ways I can think of (before discussing pure XML
databases) is SAX or DOM XPath.
SAX is probably the simplest (hence the name, Simple Api for XML).
Basically you just write a filter function which is targetted at a
particular XML construct. Lets say you want to target an ELEMENT
construct. You then write a handler function that deals with elements.
When you execute the SAX parser, it will call this function everytime it
hits an element as it "scans" down a document. Simply put an "if"
statement in the function which has your criteria, then you can add the
content to some sort of stack (array) when the condition is met.
http://www.php.net/xml
This can be tricky since may probably need to use
xml_set_character_data_handler() instead of xml_set_element_handler()
depending on your target info.
DOM is what I always use. So basically you creat a PHP DOM XML object
instance based on your document, you then use an XPath function on that
object to return you a nodeset. You can extract your data from this
nodeset. THis is generally how I do it.
http://www.php.net/manual/en/function.xpath-eval.php
My framework makes this easy. (
http://xao-php.sourceforge.net)
$objDom = new DomDoc($xmlData,XAO_DOC_DATA);
$arrNodes = $objDom->arrNdXPath($criteria_xpath_expression);
and then of course, for each node you can run get_content() on it.
http://www.php.net/manual/en/functio...et-content.php
OK. The last option is the most ideal. **Pure XML databases** have
XQuery which goes beyond XPath and incorporate more SQL-type
functionality (even though XPath by itself is equal to the task). The
difference is that your query can span any amount of XML documents or
document collections :)
Unfortunately I am out of time so maybe an article on that later. The
best product I have seen so far is Tamino by Software AG but an
unlimited licence is not cheap (think tens of thousands $$$ per CPU). On
the other hand, there are 2 strong open-source options.
http://eXist.sourceforge.net
and
http://xml.apache.org/xindice
GO LOOK AT THE DOCUMENTATION. You will find that these databases meet
your needs exactly. Both can be accessed from PHP scripts which
implement XML-RPC. This will be the next feature to go into my framework.
good luck.
PS.
Tamino can backend onto other information systems such as MySQL and you
can assign columns to tags/attributes or whatever in your database
documents. XQueries (and other access methods) then pick up the data in
your database completely transparently. Tamino is "THE KILLER PRODUCT"
in my opinion, but it is like Oracle or IBM in the $$$ department :(
One access method is webdav, which is great for win XP users who can
effectively "mount" in in windows explorer. Of course same applies for
unix, but windows gumbies love it :)