dou72260 2013-06-26 15:18
浏览 33
已采纳

DOMNode :: getNodePath:它是否考虑了命名空间?

I have a weird behaviour here.

I have a snippet of code that I used to ran under zend framework 2 php (don't remember what version of it) and everything works like a charm.

For some reasons I'd changed this and run it under local PHP (5.3.25) as I started a new project over Symfony2, but I've took some snippet of code that could be useful for me.

However, now this code ins't working anymore:

$document = new \DOMDocument();
$document->loadXML($data);
$xpath = new \DOMXPath($document);
$xpath->registerNamespace('ns',"http://somenamespace.org/FOLDER/2013"); //casual ns used for this example
$foo = $xpath->query('//ns:AvailStatusMessage');
//some elaboration here
$barXPathString = $foo->getElementsByTagName('bar')->item(0)->getNodePath();
$foobar = $xpath->query($barXPathString .'/*[@fooBarAttribute="Available"]');
//some elaboration here

Result is that $foobar is null (with previous version it wasn't). Obviously my code isn't changed. Although XML is important, this question ins't about "query correctness" (I know that is correct, because it worked into previous version) but about the following:

If I do: $this->logger->debug('X PATH STRING: '.$barXPathString); result is

/OTA_HotelAvailGetRS/AvailStatusMessages/AvailStatusMessage/bar

but there isn't namespace. So i was wondering if between PHP version, this implementation is changed (don't know if this is issue maker, but I'm running crazy)

Is anyone aware of this?

  • 写回答

1条回答 默认 最新

  • dpgui8229808 2013-06-27 05:34
    关注

    Regardless of how getNodePath() works, trying to build up the XPath by manually cobbling it together looks like the wrong approach here. DOMXPath::query accepts a context node argument, and you should use that:

    $firstBar = $foo->getElementsByTagName('bar')->item(0);
    $foobar = $xpath->query('*[@fooBarAttribute="Available"]', $firstBar);
    

    Also as hakre points out, your code is a bit confusing. DOMNodeList doesn't have a getElementsByTagName() method, so I'm not entirely sure what you're trying to do here:

    $barXPathString = $foo->getElementsByTagName('bar')->item(0)->getNodePath();
    

    Wouldn't it just be:

    $barXPathString = $foo->item(0)->getNodePath();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C