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 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退