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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度