doujing1858 2017-01-05 08:50
浏览 51
已采纳

php xpath与dom如何编写更短的查询

The xml file that I need to get out data is large and has lot of inner children in children. The XML on what I need to query looks like in the picture and it has more Ref children in company. xml structure

I need to get the company node that has the correct Info->ID. That node has 3 Ref nodes and I need to get Date from the one that has the correct Who.

I got this working with this ugly code:

$query_pod = "//Seller/Company/Info/ID[ID = 'IV'] | //Seller/Company/Ref/Who[Who = 'VA'] | //Seller/Company/Ref/Date";
foreach ($xpath->query( $query_pod ) as $pod)
{
    $pod_dates = $xpath->query( "//Seller/Company/Ref/Who[Who = 'VA'] | //Seller/Company/Ref/Date", $pod );
    $pod_date = $pod_dates->item(0)->nodeValue;
}

I tryed it shorter but I cant get the select element in [] like this:

$query_pod = "//Seller/Company/Info[ID = 'IV']";
foreach ($xpath->query( $query_pod ) as $pod)
{
    $pod_dates = $xpath->query( "//Seller/Company/Ref/Date[Who = 'VA'], $pod );
    $pod_date = $pod_dates->item(0)->nodeValue;
}

Can someone help? I'm new to xpath.

  • 写回答

1条回答 默认 最新

  • duanhuan6336 2017-01-05 13:08
    关注

    You second expression (the on inside the loop) does not depend on the context. A slash (/) at the begin of a location path means that is absolute, it starts at the document node. [] are conditions that filter the result from the previous location path. They are full Xpath expression themselves. So no, it is not always possible to shorten the Xpath expressions. But in you case you can fetch the company node as a list and then the details directly using DOMXpath::evaluate(). Unlike DOMXpath::query(), DOMXpath::evaluate() can return node lists or scalar values. It depending on the expression.

    $xml = <<<'XML'
    <Seller>
      <Company>
        <Info> 
          <ID>123</ID>
          <Address>Some street, somewhere</Address>
        </Info>
        <Ref>
          <Who>456</Who>
          <Date>2017-01-05T01:59Z</Date>
        </Ref>
      </Company>
    </Seller>
    XML;
    
    $document = new DOMDocument();
    $document->loadXml($xml);
    $xpath = new DOMXpath($document);
    
    foreach ($xpath->evaluate('/Seller/Company[Info/ID = "123"]') as $company) {
      var_dump(
        $xpath->evaluate('string(Info/ID)', $company),
        $xpath->evaluate('string(Ref/Date)', $company)
      );
    }
    

    Output:

    string(3) "123"
    string(17) "2017-01-05T01:59Z"
    

    If you only need a single value you can fetch it directly:

    var_dump(
      $xpath->evaluate('string(/Seller/Company[Info/ID = "123"]/Ref/Date)')
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂