xml xquery and php

M

Manish

I am new to XML. Initially I was saving all the settings in either PHP
or text files or database tables and use to parse the config variables
from files or query from the database. Now I want to keep all the
settings in the XML file and get the setting values, user datas (as
database is not involved and all the results are stored in XML file
itself) from it.

Referring to http://www.w3schools.com/xquery/xquery_example.asp, it
gives sample xml file and also query. But I don't know how to execute
it.
-------------------------------------------------------------------------------------------------------------

"books.xml":

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>

<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>

<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>

<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>

</bookstore>

-------------------------------------------------------------------------------------------------------------

doc("books.xml")/bookstore/book[price<30]

The XQuery above will extract the following:

<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>

-------------------------------------------------------------------------------------------------------------

I have a PHP 5 installed on the computer. I want to know, provided the
XML file, how do I execute the query and get the results. Are there
some API, Packages avaailable.

Thanks.

Manish
 
M

Martin Honnen

Manish wrote:

doc("books.xml")/bookstore/book[price<30]

The XQuery
I have a PHP 5 installed on the computer. I want to know, provided the
XML file, how do I execute the query and get the results. Are there
some API, Packages avaailable.

PHP 5 does not support XQuery, and XQuery is not yet a W3C recommendation.
PHP supports XPath 1.0 however so using the DOM and XPath support in PHP
5 you can do e.g.

<?php
$xmlDocument = new DOMDocument();
if ($xmlDocument->load('file.xml')) {
$xpath = new DOMXPath($xmlDocument);
$nodeList = $xpath->query('/bookstore/book[price<30]', $xmlDocument);
header('Content-Type: text/plain');
foreach ($nodeList as $node) {
echo $xmlDocument->saveXML($node) . "\r\n";
}
}
?>

Setting Content-Type as text/plain and simply echoing the XML to the
user agent is probably not what you want to do in a real application but
it allows you to see the XML of the nodes the XPath selection returned
for that sample above.

See the PHP documentation
<http://www.php.net/manual/en/function.dom-domxpath-query.php>
 
M

Manish

Hi Martin,

Thanks for the Post. It worked. As I am new to XML, I will need to
study the topic and figure out the syntax format for inserting new
data, update, and delete the information in the XML file.

Regards.

Manish
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top