xml xquery and php

Discussion in 'XML' started by Manish, Jul 17, 2006.

  1. Manish

    Manish Guest

    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
     
    Manish, Jul 17, 2006
    #1
    1. Advertising

  2. 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>


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jul 17, 2006
    #2
    1. Advertising

  3. Manish

    Manish Guest

    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
     
    Manish, Jul 18, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page