SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Amend PHP with Xpath sorting

    Hi

    I've been trying to add some sort of Xpath to sort my XML with PHP, seems to be returning the correct amount of elements based on the criteria, but now no values are showing. No hotel name, no img, just empty div containers.

    Can anybody spot what is causing the empty values, what am I doing wrong?

    PHP Code:
    <?php $i=1; foreach ($hotels->hotel->xpath('//hotel/location[. ="some location"]') as $hotel): ?>
                  <?php $clss= ($i == 0)? 'class="last"':'' ?>
                  <div <?php echo $clss ?> >
                        <img src="images/<? echo $hotel->img->name ?>">
                        <h3><?php echo $hotel->name ?></h3>
                        <p><?php echo $hotel->description ?></p>
                        <p><b>Rating:</b> <?php echo $hotel->star ?></p>
                        <p><b>Location:</b> <?php echo $hotel->location ?></p>
                </div>
                <? $i++;?>
                <? endforeach ?>
    If I use the below everything works ok but no Xpath sorting.

    PHP Code:
    <?php $i=1; foreach ($hotels->hotel as $hotel): ?>

    Barry
    The more you learn.... the more you learn there is more to learn.

  2. #2
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can anybody shed some light on this?
    50 viewings no reply

    I might revert back to using a MySQL DB and leave extracting my data from XML altogether.
    Any views on using MySQL over XML?

    I was hoping to get a better understanding of using XML and querying this with Xpath as this is just a small project I'm working on, creating a full DB seems a bit overkill.

    Barry
    The more you learn.... the more you learn there is more to learn.

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by computerbarry View Post
    Can anybody shed some light on this?
    50 viewings no reply

    I might revert back to using a DB and leave extracting my data from XML altogether.
    Any views on using DB over XML?

    Barry
    There are two things I'd do to try and sort out your problem,
    1) output var_dump($hotels->hotel->xpath('//hotel/location[. ="some location"]')); and post it here, I'm curious as to what that returns
    2) output var_dump($hotels->hotel) and compare it to the output of #1

    However, regardless, XML is a terrible data source to use in real time, it requires I/O operations were are slower than database interactions. The only time I ever use XML from an I/O standpoint is in nightly jobs that then process the data from the XML file and store that data in other formats (serialized json array, database, etc) anything that makes it more convenient for PHP to use.

  4. #4
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers cpradio

    var_dump($hotels->hotel->xpath('//hotel/location[. ="some location"]'));
    Code:
    array(6) {
      [0]=&gt;
      object(SimpleXMLElement)#5 (0) {
      }
      [1]=&gt;
      object(SimpleXMLElement)#6 (0) {
      }
      [2]=&gt;
      object(SimpleXMLElement)#4 (0) {
      }
      [3]=&gt;
      object(SimpleXMLElement)#2 (0) {
      }
      [4]=&gt;
      object(SimpleXMLElement)#3 (0) {
      }
      [5]=&gt;
      object(SimpleXMLElement)#10 (0) {
      }
    }
    var_dump($hotels->hotel);
    Code:
    object(SimpleXMLElement)#8 (6) {
      ["img"]=&gt;
      object(SimpleXMLElement)#6 (2) {
        ["name"]=&gt;
        string(26) "hotel_one.jpg"
        ["alt"]=&gt;
        string(12) "hotel one"
      }
      ["name"]=&gt;
      string(6) "hotel1"
      ["description"]=&gt;
      string(23) "Accommodation in the UK"
      ["address"]=&gt;
      object(SimpleXMLElement)#5 (3) {
        ["number"]=&gt;
        string(1) "3"
        ["street"]=&gt;
        string(10) "steers way"
        ["postcode"]=&gt;
        string(6) "W1 8LW"
      }
      ["star"]=&gt;
      string(1) "4"
      ["location"]=&gt;
      string(2) "uk"
    }

    However, regardless, XML is a terrible data source
    I respect that and kind of agree but was hoping to get an understand of this, lots of jobs require XML data manipulation using Xpath and I have worked in previous roles (Interwoven Teamsite CMS in particular) which uses XML as its main data storage and styled using XSL transformations.

    What do you think of the above, whats causing the issue?

    And would you say json is the better option over XML?

    Barry
    The more you learn.... the more you learn there is more to learn.

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Okay, I think your Xpath is incorrect. As you are only getting the location element returned.

    Try this:
    PHP Code:
    $hotels->hotel->xpath('//hotel[descendant::location[. ="some location"]]'
    Place that in the var_dump as well to see what that gives.

  6. #6
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool

    That works brilliantly.
    The var_dump is array(6) now with all 6 hotels details as the above output, looks good thank you.

    Now my next problem
    How do I now sort these hotels by star rating?
    And what if the value is budget and not a integer?

    Cheers Barry
    The more you learn.... the more you learn there is more to learn.

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    xpath alone can't do sorting. That has to be done via XSL or you have to loop through and sort manually. What you have selectively done up to this point is filter the data.

    I haven't done a lot of sorting via XSL, again because it seems cumbersome to me (don't get me wrong, I love XML over CSV, flat files, etc, but where you are heading with it seems to be more fitted for a relational database).

    You can check out http://stackoverflow.com/questions/4...lues-via-xpath

    You will see that you can sort across multiple elements and you get to define the data type of the element (so it may be a string, but if it is numeric, you can convert it to a number). The select attribute seems to define the element name to use, then there is the data-type and lastly the order.

    If you mock something up and it isn't doing what you want, post it and I'll be glad to look it over and see if I spotted any mistakes You can find an example in the comments for applying XSL to SimpleXMLElement at http://php.net/manual/en/book.xsl.php

    Edit:

    Added link to PHP manual where the comments show how to apply an XSL

  8. #8
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers cpradio

    I haven't done much XSL myself only worked with other peoples code and modified small snippets etc.
    And yes that's my predicament right now - to use a mysql db and be done with it, I can easily get the results I'm after but, ha I was hoping to learn something new and enhance my skillset and did thing this was a good way to do things.

    Thanks for the link, I think I'll review what works best for this small project and see where it takes me.
    Cheers again for the help and I'll be sure to post any further code for review if I come unstuck as I move forward.

    Barry
    The more you learn.... the more you learn there is more to learn.

  9. #9
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    If XSLT looks like too much hard work, remember that SimpleXML's xpath() returns an array and PHP has several functions to sort arrays in a variety of ways. Here's an example using usort().

    PHP Code:
    $hotels_at_location $hotels->xpath('hotel[location="some location"]');
    usort($hotels_at_location, function ($a$b) { return (int) $b->star - (int) $a->star; });

    foreach (
    $hotels_at_location as $hotel) {
        
    // Do something useful here

    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •