1

Below is my xml with namespace

    <?xml version="1.0" encoding="UTF-8"?>
    <hotels xmlns="http://www.test.com/schemas/messages" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://www.test.com/schemas/messages">
    <hotel >
        <rooms>
            <room>
                <rates>
                    <rate id="1" adults="1" child="0"></rate>
                    <rate id="2" adults="2" child="0"></rate>
                    <rate id="3" adults="1" child="0"></rate>
                </rates>
            </room>
            <room>
                <rates>
                    <rate id="4" adults="1" child="0"></rate>
                    <rate id="5" adults="2" child="0"></rate>
                    <rate id="6" adults="2" child="0"></rate>
                </rates>
            </room>
        </rooms>
    </hotel>
    </hotels>

i trying below php code (xpath) using foreach to get values of the ratenode

$xd = simplexml_load_file('C:/inetpub/vhosts/test.com/data_download/q.xml');
    $xd->registerXPathNamespace("n", "http://www.test.com/schemas/messages");       

foreach($xd->xpath("//n:hotels/n:hotel") as $xd_item)
        {
            echo 'item - A';
            foreach($xd_item->xpath("rooms/room") as $xd_room)
            {
                foreach($xd_room->xpath("rates/rate") as $xd_rate)
                {
                    echo 'rate - C';
                }
            }
        }

In the foreach of $xd_item is not working. I mean the 2nd foreach its end with the value "echo 'item - A';" anyone can help me?

Note
  • 177
  • 1
  • 1
  • 9
  • Not sure how you think `echo 'rate - C'` will get the "values of the ratenode", but note that `rooms`, `room`, `rates`, and `rate` should all be prefixed by `n:` because they too are in the default `http://www.test.com/schemas/messages` namespace. – kjhughes Jan 31 '16 at 04:57
  • thanks, How i go in side the rate some thing like echo `$xd_rate->attributes()->id` – Note Jan 31 '16 at 05:11
  • I would suggest you instead use DomDocument: http://www.w3schools.com/php/php_xml_dom.asp, as it is match more easy to parse xml using it – Armen Jan 31 '16 at 05:45
  • thanks, Is possible to write conditions like `$xd->xpath("rates/rate[@id='1']")` in dom – Note Jan 31 '16 at 05:52
  • No in DomDocument you have to loop over nodes and check its attribute, you can use instead DomXpath look on first answer here: http://stackoverflow.com/questions/6827871/php-domdocument-get-node-value-where-attribute-value-is – Armen Jan 31 '16 at 05:57

2 Answers2

0

The problem with your codes is, as mentioned by @kjhughes in his comment, that some elements are in the default namespace but your XPath missed to use the corresponding prefix on those elements. Also, your code prints static literal strings i.e 'item - A' and 'rate - C', not any part of the XML being parsed.

" Is possible to write conditions like $xd->xpath("rates/rate[@id='1']") in dom"

Yes it is possible. It is also possible using SimpleXML, for example :

$xd->registerXPathNamespace("n", "http://www.test.com/schemas/messages");
foreach($xd->xpath("//n:rate[@id='1']") as $rate){
    echo $rate["id"] .", ". $rate["adults"] .", ". $rate["child"] ."\r\n";
}

eval.in demo

output :

//id, adults, rate
1, 1, 0
har07
  • 88,338
  • 12
  • 84
  • 137
0

The variable $xd_item is of type SimpleXMLElement from which you can access its room properties like this for example:

$xd_item->rooms->room

This will return an object of type SimpleXMLElement from which you can get the rates and you can loop using a foreach. The values that you want from the 'rate' are in the attributes

For example:

$xd = simplexml_load_file('C:/inetpub/vhosts/test.com/data_download/q.xml');
$xd->registerXPathNamespace("n", "http://www.test.com/schemas/messages");

foreach($xd->xpath("//n:hotels/n:hotel") as $xd_item)
{
    foreach($xd_item->rooms->room as $room) {
        foreach ($room->rates->rate as $rate) {
            echo sprintf(
                'id: %s<br>adults: %s<br>child: %s<br><br>',
                $rate->attributes()->id->__toString(),
                $rate->attributes()->adults->__toString(),
                $rate->attributes()->child->__toString()
            );
        }
    }
}
The fourth bird
  • 154,723
  • 16
  • 55
  • 70