2015-08-08 50 views
2

我正在修复另一家公司的模块,我无法解释为什么来自XML的xPath会给我一个空的结果。XML,xpath空结果

这里是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> 

这里是代码:

$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; 
} 

我所看到的是,$ tipsa_num_albaran具有正确的值,但$ tipsa_num_seguimiento是空的。这两个值都在同一深度,并在XML的同一分支,所以我不明白为什么我的第二个值是空的。

感谢

+0

无法重现,两个结果有自己的价值观:https://开头eval.in/414052 - 你可能没有正确地分配值或变量名称?它们看起来很长,所以可能会将错误报告提升到最高级别并检查警告和通知:[如何在PHP中获取有用的错误消息?](http://stackoverflow.com/q/845021/367456) – hakre

回答

1

一个可能的解释是,你是如何使用的变量:$tipsa_num_albaran$tipsa_num_seguimiento。由于SimpleXMLElement s,when casted to a string,这些对象将会:

返回直接在此元素中的文本内容。不返回此元素的子元素内的文本内容。

我假设这些是你正在寻找的值(不是对象本身),那么试试这个来代替:

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

解决了我的问题。谢谢! – Serpes