dongxi1680 2015-08-08 15:25
浏览 40
已采纳

XML,xpath为空结果

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

  • 写回答

1条回答 默认 最新

  • duanli9569 2015-08-08 18:24
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?