Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    May 2009
    Posts
    160
    Thanks
    71
    Thanked 1 Time in 1 Post

    PHP Xpath - return whole block

    Hi, having trouble with something. I have the following function

    Code:
    function getClassPriceFromResponse($serverResponsePricing){
        $dom = new DOMDocument('1.0', 'UTF-8');
        $dom->loadXML($serverResponsePricing);
    
        $xpath = new DOMXPath($dom);
        $xpath->registerNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
        $xpath->registerNamespace('test', 'http://www.test.com/test_v5_0');
        $priceSolution = $xpath->evaluate('//test:PriceRsp/test:PriceResult/test:PricingSolution');
    
        return $priceSolution;
    }
    The passed variable $serverResponsePricing is a load of XML code. I load it in, register the namespaces, and then evaluate it. The XML pretty much looks like the following in its simple form.
    Code:
    <SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
      <SOAP:Body>
        <test:PriceRsp xmlns:test="http://www.test.com/test_v5_0">
           <test:PriceResult>
              <test:PricingSolution>
                     
              <test:PricingSolution>
           </test:PriceResult>
        </test:PriceRsp>
      </SOAP:Body>
    </SOAP:Envelope>
    What I am attempting to do with the first function is return PricingSolution and anything contained within it.

    What I am doing doesnt seem to work though, I don't know if I really need to evaluate it? I am used to getting hold of single nodes using getAttribute, so trying to return the whole block is a bit different to me.

    Any advise appreciated.

    Cheers

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    This appears to be working for me:
    Code:
    // soap.xml
    <?xml version="1.0"?>
    <SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
      <SOAP:Body>
        <test:PriceRsp xmlns:test="http://www.test.com/test_v5_0">
           <test:PriceResult>
              <test:PricingSolution>
                     <![CDATA[Something here]]>
              </test:PricingSolution>
           </test:PriceResult>
        </test:PriceRsp>
      </SOAP:Body>
    </SOAP:Envelope>
    PHP Code:
    <?php

    $dom 
    = new DOMDocument('1.0');
    $dom->load(__DIR__ '/soap.xml');

    $xpath = new DOMXPath($dom);
    $xpath->registerNamespace('soap''http://schemas.xmlsoap.org/soap/envelope/');
    $xpath->registerNamespace('test''http://www.test.com/test_v5_0');
    $priceSolution $xpath->evaluate('//test:PriceRsp/test:PriceResult/test:PricingSolution');
    foreach (
    $priceSolution AS $itm)
    {
        print 
    $itm->nodeValue;
    }
    Results:
    Code:
                     Something here
    Are you sure that the $serverResponsePricing is the entire xml string? Also, I assumed that the pricingsolution element was a typo above; I added the end tag to it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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