adamZ88 has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I need to retrieve the contents of what I consider a complex soap response.The below response is a less complex one and returns a description element and the one line of code preceding it shows how I retrieve that value

my $_sDeviceDescription=$sEntireDeviceDescription->valueof('//return/row/description');
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envel +ope/"> <soapenv:Body> <ns:executeSQLQueryResponse xmlns:ns="http://www.cisco.com/AXL/A +PI/8.5"> <return> <row> <description>TB3 - Test Phone - 20078</description> </row> </return> </ns:executeSQLQueryResponse> </soapenv:Body> </soapenv:Envelope>

Now, this is my more complex response. This query is done on Cisco Unified Communications Manager to get the registration status of a device(a phone). In CUCM there are many nodes to a cluster, and a phone can be registered to most of those nodes, that is why this query is done on all nodes and thus presents the results of each node for the same device. I need to return the value of the <Status> Element for those nodes that Have the <ReturnCode> element value of "Ok". To further explain, this(these) nodes are nodes where the phone is registered or was registered at one point. For the Nodes that have the <ReturnCode> value of "Ok" in this example, the value of <Status> is Unregistered, that is the value I would need to assign to a variable like in the less complex example above and later print that value. Thanks.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envel +ope/"> <soapenv:Body> <ns1:selectCmDeviceResponse xmlns:ns1="http://schemas.cisco.com/ +ast/soap"> <ns1:selectCmDeviceReturn> <ns1:SelectCmDeviceResult> <ns1:TotalDevicesFound>2</ns1:TotalDevicesFound> <ns1:CmNodes> <ns1:item> <ns1:ReturnCode>NotFound</ns1:ReturnCode> <ns1:Name>10.10.10.10</ns1:Name> <ns1:NoChange>false</ns1:NoChange> <ns1:CmDevices xsi:nil="1" xmlns:xsi="http://www. +w3.org/2001/XMLSchema-instance"/> </ns1:item> <ns1:item> <ns1:ReturnCode>NotFound</ns1:ReturnCode> <ns1:Name>10.10.10.11</ns1:Name> <ns1:NoChange>false</ns1:NoChange> <ns1:CmDevices xsi:nil="1" xmlns:xsi="http://www. +w3.org/2001/XMLSchema-instance"/> </ns1:item> <ns1:item> <ns1:ReturnCode>NotFound</ns1:ReturnCode> <ns1:Name>10.10.10.12</ns1:Name> <ns1:NoChange>true</ns1:NoChange> <ns1:CmDevices xsi:nil="1" xmlns:xsi="http://www. +w3.org/2001/XMLSchema-instance"/> </ns1:item> <ns1:item> <ns1:ReturnCode>NotFound</ns1:ReturnCode> <ns1:Name>10.10.10.13</ns1:Name> <ns1:NoChange>false</ns1:NoChange> <ns1:CmDevices xsi:nil="1" xmlns:xsi="http://www. +w3.org/2001/XMLSchema-instance"/> </ns1:item> <ns1:item> <ns1:ReturnCode>Ok</ns1:ReturnCode> <ns1:Name>10.10.10.14</ns1:Name> <ns1:NoChange>false</ns1:NoChange> <ns1:CmDevices> <ns1:item> <ns1:Name>UPCTEST</ns1:Name> <ns1:DirNumber>20078-UnRegistered</ns1:DirN +umber> <ns1:DeviceClass>Phone</ns1:DeviceClass> <ns1:Model>503</ns1:Model> <ns1:Product>390</ns1:Product> <ns1:BoxProduct>0</ns1:BoxProduct> <ns1:Httpd>No</ns1:Httpd> <ns1:RegistrationAttempts>0</ns1:Registrati +onAttempts> <ns1:IsCtiControllable>true</ns1:IsCtiContr +ollable> <ns1:LoginUserId>sampleuser</ns1:LoginUserI +d> <ns1:Status>UnRegistered</ns1:Status> <ns1:StatusReason>8</ns1:StatusReason> <ns1:PerfMonObject>2</ns1:PerfMonObject> <ns1:DChannel>0</ns1:DChannel> <ns1:Description>TB3 - Test Phone - 20078</ +ns1:Description> <ns1:H323Trunk> <ns1:ConfigName xsi:nil="1" xmlns:xsi="h +ttp://www.w3.org/2001/XMLSchema-instance"/> <ns1:TechPrefix xsi:nil="1" xmlns:xsi="h +ttp://www.w3.org/2001/XMLSchema-instance"/> <ns1:Zone xsi:nil="1" xmlns:xsi="http:// +www.w3.org/2001/XMLSchema-instance"/> <ns1:RemoteCmServer1 xsi:nil="1" xmlns:x +si="http://www.w3.org/2001/XMLSchema-instance"/> <ns1:RemoteCmServer2 xsi:nil="1" xmlns:x +si="http://www.w3.org/2001/XMLSchema-instance"/> <ns1:RemoteCmServer3 xsi:nil="1" xmlns:x +si="http://www.w3.org/2001/XMLSchema-instance"/> <ns1:AltGkList xsi:nil="1" xmlns:xsi="ht +tp://www.w3.org/2001/XMLSchema-instance"/> <ns1:ActiveGk xsi:nil="1" xmlns:xsi="htt +p://www.w3.org/2001/XMLSchema-instance"/> <ns1:CallSignalAddr xsi:nil="1" xmlns:xs +i="http://www.w3.org/2001/XMLSchema-instance"/> <ns1:RasAddr xsi:nil="1" xmlns:xsi="http +://www.w3.org/2001/XMLSchema-instance"/> </ns1:H323Trunk> <ns1:TimeStamp>1489800832</ns1:TimeStamp> <ns1:Protocol>SIP</ns1:Protocol> <ns1:NumOfLines>1</ns1:NumOfLines> <ns1:LinesStatus> <ns1:item> <ns1:DirectoryNumber>20078</ns1:Direc +toryNumber> <ns1:Status>UnRegistered</ns1:Status> </ns1:item> </ns1:LinesStatus> <ns1:ActiveLoadID>Jabber_for_Windows-11.8.2 +.50390</ns1:ActiveLoadID> <ns1:InactiveLoadID>Jabber_for_Windows-11.8 +.2.50390</ns1:InactiveLoadID> <ns1:DownloadStatus>Unknown</ns1:DownloadSt +atus> <ns1:DownloadFailureReason xsi:nil="1" xmln +s:xsi="http://www.w3.org/2001/XMLSchema-instance"/> <ns1:DownloadServer xsi:nil="1" xmlns:xsi=" +http://www.w3.org/2001/XMLSchema-instance"/> <ns1:IPAddress> <ns1:item> <ns1:IP>10.171.39.29</ns1:IP> <ns1:IPAddrType>ipv4</ns1:IPAddrType> <ns1:Attribute>Unknown</ns1:Attribute +> </ns1:item> </ns1:IPAddress> </ns1:item> </ns1:CmDevices> </ns1:item> <ns1:item> <ns1:ReturnCode>NotFound</ns1:ReturnCode> <ns1:Name>10.10.10.15</ns1:Name> <ns1:NoChange>true</ns1:NoChange> <ns1:CmDevices xsi:nil="1" xmlns:xsi="http://www. +w3.org/2001/XMLSchema-instance"/> </ns1:item> <ns1:item> <ns1:ReturnCode>NotFound</ns1:ReturnCode> <ns1:Name>10.10.10.16</ns1:Name> <ns1:NoChange>false</ns1:NoChange> <ns1:CmDevices xsi:nil="1" xmlns:xsi="http://www. +w3.org/2001/XMLSchema-instance"/> </ns1:item> <ns1:item> <ns1:ReturnCode>Ok</ns1:ReturnCode> <ns1:Name>10.10.10.17</ns1:Name> <ns1:NoChange>false</ns1:NoChange> <ns1:CmDevices> <ns1:item> <ns1:Name>UPCTEST</ns1:Name> <ns1:DirNumber>20078-UnRegistered</ns1:DirN +umber> <ns1:DeviceClass>Phone</ns1:DeviceClass> <ns1:Model>503</ns1:Model> <ns1:Product>390</ns1:Product> <ns1:BoxProduct>0</ns1:BoxProduct> <ns1:Httpd>No</ns1:Httpd> <ns1:RegistrationAttempts>0</ns1:Registrati +onAttempts> <ns1:IsCtiControllable>true</ns1:IsCtiContr +ollable> <ns1:LoginUserId>sampleuser</ns1:LoginUserI +d> <ns1:Status>UnRegistered</ns1:Status> <ns1:StatusReason>6</ns1:StatusReason> <ns1:PerfMonObject>2</ns1:PerfMonObject> <ns1:DChannel>0</ns1:DChannel> <ns1:Description>TB3 - Test Phone - 20078</ +ns1:Description> <ns1:H323Trunk> <ns1:ConfigName xsi:nil="1" xmlns:xsi="h +ttp://www.w3.org/2001/XMLSchema-instance"/> <ns1:TechPrefix xsi:nil="1" xmlns:xsi="h +ttp://www.w3.org/2001/XMLSchema-instance"/> <ns1:Zone xsi:nil="1" xmlns:xsi="http:// +www.w3.org/2001/XMLSchema-instance"/> <ns1:RemoteCmServer1 xsi:nil="1" xmlns:x +si="http://www.w3.org/2001/XMLSchema-instance"/> <ns1:RemoteCmServer2 xsi:nil="1" xmlns:x +si="http://www.w3.org/2001/XMLSchema-instance"/> <ns1:RemoteCmServer3 xsi:nil="1" xmlns:x +si="http://www.w3.org/2001/XMLSchema-instance"/> <ns1:AltGkList xsi:nil="1" xmlns:xsi="ht +tp://www.w3.org/2001/XMLSchema-instance"/> <ns1:ActiveGk xsi:nil="1" xmlns:xsi="htt +p://www.w3.org/2001/XMLSchema-instance"/> <ns1:CallSignalAddr xsi:nil="1" xmlns:xs +i="http://www.w3.org/2001/XMLSchema-instance"/> <ns1:RasAddr xsi:nil="1" xmlns:xsi="http +://www.w3.org/2001/XMLSchema-instance"/> </ns1:H323Trunk> <ns1:TimeStamp>1489766440</ns1:TimeStamp> <ns1:Protocol>SIP</ns1:Protocol> <ns1:NumOfLines>1</ns1:NumOfLines> <ns1:LinesStatus> <ns1:item> <ns1:DirectoryNumber>20078</ns1:Direc +toryNumber> <ns1:Status>UnRegistered</ns1:Status> </ns1:item> </ns1:LinesStatus> <ns1:ActiveLoadID>Jabber_for_Windows-11.8.2 +.50390</ns1:ActiveLoadID> <ns1:InactiveLoadID>Jabber_for_Windows-11.8 +.2.50390</ns1:InactiveLoadID> <ns1:DownloadStatus>Unknown</ns1:DownloadSt +atus> <ns1:DownloadFailureReason xsi:nil="1" xmln +s:xsi="http://www.w3.org/2001/XMLSchema-instance"/> <ns1:DownloadServer xsi:nil="1" xmlns:xsi=" +http://www.w3.org/2001/XMLSchema-instance"/> <ns1:IPAddress> <ns1:item> <ns1:IP>10.171.39.29</ns1:IP> <ns1:IPAddrType>ipv4</ns1:IPAddrType> <ns1:Attribute>Unknown</ns1:Attribute +> </ns1:item> </ns1:IPAddress> </ns1:item> </ns1:CmDevices> </ns1:item> <ns1:item> <ns1:ReturnCode>NotFound</ns1:ReturnCode> <ns1:Name>10.10.10.18</ns1:Name> <ns1:NoChange>true</ns1:NoChange> <ns1:CmDevices xsi:nil="1" xmlns:xsi="http://www. +w3.org/2001/XMLSchema-instance"/> </ns1:item> <ns1:item> <ns1:ReturnCode>NotFound</ns1:ReturnCode> <ns1:Name>10.10.10.19</ns1:Name> <ns1:NoChange>false</ns1:NoChange> <ns1:CmDevices xsi:nil="1" xmlns:xsi="http://www. +w3.org/2001/XMLSchema-instance"/> </ns1:item> <ns1:item> <ns1:ReturnCode>NotFound</ns1:ReturnCode> <ns1:Name>10.10.10.20</ns1:Name> <ns1:NoChange>false</ns1:NoChange> <ns1:CmDevices xsi:nil="1" xmlns:xsi="http://www. +w3.org/2001/XMLSchema-instance"/> </ns1:item> <ns1:item> <ns1:ReturnCode>NotFound</ns1:ReturnCode> <ns1:Name>10.10.10.21</ns1:Name> <ns1:NoChange>true</ns1:NoChange> <ns1:CmDevices xsi:nil="1" xmlns:xsi="http://www. +w3.org/2001/XMLSchema-instance"/> </ns1:item> <ns1:item> <ns1:ReturnCode>NotFound</ns1:ReturnCode> <ns1:Name>10.10.10.22</ns1:Name> <ns1:NoChange>true</ns1:NoChange> <ns1:CmDevices xsi:nil="1" xmlns:xsi="http://www. +w3.org/2001/XMLSchema-instance"/> </ns1:item> <ns1:item> <ns1:ReturnCode>NotFound</ns1:ReturnCode> <ns1:Name>10.10.10.23</ns1:Name> <ns1:NoChange>true</ns1:NoChange> <ns1:CmDevices xsi:nil="1" xmlns:xsi="http://www. +w3.org/2001/XMLSchema-instance"/> </ns1:item> </ns1:CmNodes> </ns1:SelectCmDeviceResult> <ns1:StateInfo><![CDATA[<StateInfo><Node Name="10.10.10.10 +" SubsystemStartTime="1478227537" StateId="55886" TotalItemsFound="0" + TotalItemsReturned="0"/><Node Name="10.10.10.11" SubsystemStartTime= +"1478222504" StateId="131271" TotalItemsFound="0" TotalItemsReturned= +"0"/><Node Name="10.10.10.12" SubsystemStartTime="0" StateId="0" Tota +lItemsFound="0" TotalItemsReturned="0"/><Node Name="10.10.10.13" Subs +ystemStartTime="1478226627" StateId="101912" TotalItemsFound="0" Tota +lItemsReturned="0"/><Node Name="10.10.10.14" SubsystemStartTime="1478 +222406" StateId="37674" TotalItemsFound="1" TotalItemsReturned="1"/>< +Node Name="10.10.10.15" SubsystemStartTime="0" StateId="0" TotalItems +Found="0" TotalItemsReturned="0"/><Node Name="10.10.10.16" SubsystemS +tartTime="1478224422" StateId="19484" TotalItemsFound="0" TotalItemsR +eturned="0"/><Node Name="10.10.10.17" SubsystemStartTime="1478225730" + StateId="13357" TotalItemsFound="1" TotalItemsReturned="1"/><Node Na +me="10.10.10.18" SubsystemStartTime="0" StateId="0" TotalItemsFound=" +0" TotalItemsReturned="0"/><Node Name="10.10.10.19" SubsystemStartTim +e="1478222539" StateId="34839" TotalItemsFound="0" TotalItemsReturned +="0"/><Node Name="10.10.10.20" SubsystemStartTime="1478224147" StateI +d="1805" TotalItemsFound="0" TotalItemsReturned="0"/><Node Name="10.1 +0.10.21" SubsystemStartTime="0" StateId="0" TotalItemsFound="0" Total +ItemsReturned="0"/><Node Name="10.10.10.23" SubsystemStartTime="0" St +ateId="0" TotalItemsFound="0" TotalItemsReturned="0"/><Node Name="10. +10.10.22" SubsystemStartTime="0" StateId="0" TotalItemsFound="0" Tota +lItemsReturned="0"/></StateInfo>]]></ns1:StateInfo> </ns1:selectCmDeviceReturn> </ns1:selectCmDeviceResponse> </soapenv:Body> </soapenv:Envelope>

Replies are listed 'Best First'.
Re: Retrieving Specific Contents of SOAP Response via SOAP::Lite
by poj (Abbot) on Mar 20, 2017 at 12:48 UTC

    Try

    #!perl use strict; use XML::LibXML; use XML::LibXML::XPathContext; my $dom = XML::LibXML->load_xml( location => 'phone.xml' ); my $xml = XML::LibXML::XPathContext->new($dom); $xml->registerNs('ns1','http://schemas.cisco.com/ast/soap'); my $xpath = "//ns1:CmNodes /ns1:item /ns1:ReturnCode[text() = 'Ok']"; my @nodes = $xml->findnodes($xpath); for my $node (@nodes){ my $name = $node->findvalue('../ns1:Name'); print $name."\n"; my @dev = $node->findnodes('../ns1:CmDevices/ns1:item'); for my $dev (@dev){ my $name = $dev->findvalue('ns1:Name'); my $status = $dev->findvalue('ns1:Status'); my $desc = $dev->findvalue('ns1:Description'); print " $name $status $desc\n"; } }
    poj

      Poj, as always, great explanation!! One single doubt, in the below what is the purpose of "'phone.xml'"

      my $dom = XML::LibXML->load_xml( location => 'phone.xml' );

        phone.xml was your soap message I put in a file. I guess you need to change that to

        my $dom = XML::LibXML->load_xml( string => $xml_string );
        

        See XML::LibXML::Parser

        poj
Re: Retrieving Specific Contents of SOAP Response via SOAP::Lite
by Anonymous Monk on Mar 20, 2017 at 01:39 UTC

    ... would need to assign to a variable like in the less complex example above and later print that value. Thanks.

    xpather.pl gives you paths if you cant write them yourself, have at it

      Thank you for your response, I will look into this.