3 Replies - 446 Views - Last Post: 01 November 2013 - 07:31 AM Rate Topic: -----

#1 nena-p  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-May 13

Problem with DOMXPath

Posted 27 October 2013 - 05:43 PM

Hi guys, having a little problem with DOMXPath and it is starting to confuse me. I receive a reply from a server and it is a big XML file. I now need to sort this file out. So I load the file, and then I begin to evaluate the top level
$xml_resp = $xpath->evaluate('//product:ProductSearchRsp/product:ProductDetailsList/product:ProductDetails');


I can then use the reponse from this to go into my loop and print out what I need
foreach ($xml_resp AS $available)
{
    printf("<br>\r\nKey %s is available <br>\r\n" . PHP_EOL, $available->getAttribute('Key'));
    printf("Product Name is %s <br>\r\n" . PHP_EOL, $available->getAttribute('Name'));
    printf("Expires %s <br>\r\n" . PHP_EOL, $available->getAttribute('Expires'));
}


Now further down the XML file, the above key links to another node containing more information. So in the foreach loop, I get the key for each product
$key = $available->getAttribute('Key');


I then use this to get into the next node section
$sXpath = "//product:ProductSearchRsp/product:ProductDetailsList/product:ProductSegment[product:ProductDetailsRef/@Key='$key']";
$productMaker = $xpath->evaluate($sXpath);
foreach ($productMaker AS $available2)
{
    $segKey = $available2->getAttribute('Key');
    printf("Product Segment Key: %s <br>\r\n" . PHP_EOL, $available2->getAttribute('Key'));
}


The important thing above is $segKey, as this will point to another record further down the XML file. This is where I am having problems. The XML I need to access is
    
<product:ProductPricingSolution Key="AEJ3kgigR/ufDeQFm1eEHQ==" TotalPrice="GBP778.10" BasePrice="GBP300.00" ApproximateTotalPrice="GBP778.10" ApproximateBasePrice="GBP300.00" Taxes="GBP478.10">
     <product:ProductPricingInfo Key="Id1dBDKMTYaF0TWG8V7GUg==" TotalPrice="GBP389.05">
           <product:BookingInfo FareInfoRef="HjDt1t+LQ4CM104VrGVJbQ==" SegmentRef="rI1OirWrQDi4/NOMWC9Qmw=="/>
     </product:ProductPricingInfo>
</product:ProductPricingSolution>


The SegmentRef in the above XML will be linked to a $segKey variable. So how can I get into the above XML? My attempt is below, any help appreciated. If any more information is needed, please ask.

$xml_resp = $xpath->evaluate('//product:ProductSearchRsp/product:ProductDetailsList/product:ProductDetails');
foreach ($xml_resp AS $available)
{
    printf("<br>\r\nKey %s is available <br>\r\n" . PHP_EOL, $available->getAttribute('Key'));
    printf("Product Name is %s <br>\r\n" . PHP_EOL, $available->getAttribute('Name'));
    printf("Expires %s <br>\r\n" . PHP_EOL, $available->getAttribute('Expires'));

    $key = $available->getAttribute('Key');

    $sXpath = "//product:ProductSearchRsp/product:ProductDetailsList/product:ProductSegment[product:ProductDetailsRef/@Key='$key']";
    $productMaker = $xpath->evaluate($sXpath);
    foreach ($productMaker AS $available2)
    {
       $segKey = $available2->getAttribute('Key');
       printf("Product Segment Key: %s <br>\r\n" . PHP_EOL, $available2->getAttribute('Key'));
    }

    //attempt below
    $sXpath2 = "//product:ProductSearchRsp/product:ProductPricingSolution/product:ProductPricingInfo[product:BookingInfo/@SegmentRef='$segKey']";
    $productMaker2 = $xpath->evaluate($sXpath2);
    foreach ($productMaker2 AS $available3)
    {
       printf("Total Price: %s <br>\r\n" . PHP_EOL, $available3->getAttribute('TotalPrice'));
    }
}


Many thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with DOMXPath

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,335
  • Joined: 08-June 10

Re: Problem with DOMXPath

Posted 28 October 2013 - 12:31 AM

question 1, what does happen? you didnít say if or how it fails.

question 2, last code, line #12, can there be more than one result from the evaluation? a loop as shown does not make any sense when you overwrite your values.

in case you donít know which node you are currently on, var_dump() should help.

in case you only get a match on <BookingInfo>, you can get the parent element through DOMNode->parentNode (itís really neat that you can map your Java≠Script DOM experience directly to PHPDOM)
Was This Post Helpful? 1
  • +
  • -

#3 nena-p  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-May 13

Re: Problem with DOMXPath

Posted 30 October 2013 - 03:28 PM

Sorry, for the delay, thank you for the reply. I have managed to solve that issue, I dont think my cache was properly clear. I do have another slight problem though. I have a query, and then I evaluate it
$cabinclasslist2 = $xpath->evaluate($sXpath2);

Now the above statement will return the node containing the contents. I can then use getAttribute to get the content. ProductClass and ProductCounts will both have content or both be empty. If empty, I need it to print out 2 empty td's. Else I need to print out the original contents. So far I have
    foreach ($cabinclasslist2 AS $available3) {
            if((string)($available3->getAttribute('ProductClass'))!='' && (string)($available3->getAttribute('ProductCounts'))!=''){
                printf("<td></td>");
                printf("<td></td>");
            }else{
                printf("<td>%s </td>" . PHP_EOL, $available3->getAttribute('ProductClass'));
                printf("<td>%s </td>" . PHP_EOL, $available3->getAttribute('ProductCounts'));
            }
        }


It still does the same as before though, and leaves an empty space in the table if the getAttributes are empty. I have checked in firebug and the td's havnt been produced.

Are you able to notice if I may be doing something wrong here?

Thanks
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,335
  • Joined: 08-June 10

Re: Problem with DOMXPath

Posted 01 November 2013 - 07:31 AM

check your condition. currently it says: if @ProductClass is not empty and @ProductCounts is not empty, print nothing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1