2

I'm fixing a module from another company and I can't explain why an xPath from a XML gives me an empty result.

Here is the XML:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:HNS="http://tempuri.org/" xmlns:v1="http://tempuri.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <SOAP-ENV:Header>
      <ROClientIDHeader xmlns="urn:DinaPaq" SOAP-ENV:mustUnderstand="0">
         <ID>{A55CF2CD-C7B8-439C-AA9E-C7970C1E8945}</ID>
      </ROClientIDHeader>
   </SOAP-ENV:Header>
   <SOAP-ENV:Body xmlns:ro="http://tempuri.org/">
      <v1:WebServService___GrabaEnvio4Response>
         <v1:strAlbaranOut>9998882267</v1:strAlbaranOut>
         <v1:dPesoVolOriOut>0</v1:dPesoVolOriOut>
         <v1:dPesoVolpesOut>1</v1:dPesoVolpesOut>
         <v1:dAltoVolpesOut>0</v1:dAltoVolpesOut>
         <v1:dAnchoVolpesOut>0</v1:dAnchoVolpesOut>
         <v1:dLargoVolpesOut>0</v1:dLargoVolpesOut>
         <v1:dPesoVolVolpesOut>0</v1:dPesoVolVolpesOut>
         <v1:dtFecEntrOut>2015-08-11T00:00:00</v1:dtFecEntrOut>
         <v1:strTipoEnvOut>N</v1:strTipoEnvOut>
         <v1:dtFecHoraAltaOut>2015-08-08T16:41:29</v1:dtFecHoraAltaOut>
         <v1:dKmsManOut>0</v1:dKmsManOut>
         <v1:boTecleDesOut>false</v1:boTecleDesOut>
         <v1:strCodAgeDesOut>029006</v1:strCodAgeDesOut>
         <v1:strCodProDesOut />
         <v1:dPorteDebOut>0</v1:dPorteDebOut>
         <v1:strCodRepOut />
         <v1:strGuidOut>{99873302-6499-44B2-9F72-C64AC3430755}</v1:strGuidOut>
         <v1:strCodSalRutaOut>1</v1:strCodSalRutaOut>
      </v1:WebServService___GrabaEnvio4Response>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

And Here is the code:

$xml = simplexml_load_string($postResult, NULL, NULL, "http://www.w3.org/2003/05/soap-envelope");
$xml->registerXPathNamespace("abc","http://tempuri.org/");

foreach ($xml->xpath('//abc:strAlbaranOut') as $item)
{
    $tipsa_num_albaran=$item;
}
foreach ($xml->xpath('//abc:strGuidOut') as $item)
{
    $tipsa_num_seguimiento=$item;
}

What I've seen is that $tipsa_num_albaran has the correct value but $tipsa_num_seguimiento is empty. Both values are at the same depth and in the same branch of the XML so I can't understand why my second value is empty.

Thanks

Serpes
  • 95
  • 1
  • 6
  • Unable to reproduce, both results have their values: https://eval.in/414052 - You perhaps do not assign the values correctly or something with your variable names? They look pretty long, so perhaps raise your error reporting to the highest level and check for warnings and notices: [How to get useful error messages in PHP?](http://stackoverflow.com/q/845021/367456) – hakre Aug 08 '15 at 18:22

1 Answers1

1

One possible explanation is how you're using the variables: $tipsa_num_albaran and $tipsa_num_seguimiento. As SimpleXMLElements, when casted to a string, these objects will:

Returns text content that is directly in this element. Does not return text content that is inside this element's children.

I'm assuming these are the values you're seeking (not the objects themselves), so try this instead:

foreach ($xml->xpath('//abc:strAlbaranOut') as $item)
{
    $tipsa_num_albaran = (string) $item;
}
foreach ($xml->xpath('//abc:strGuidOut') as $item)
{
    $tipsa_num_seguimiento = (string) $item;
}
Jacob Budin
  • 9,753
  • 4
  • 32
  • 35