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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?