解析Xpath查询的输出

时间:2014-03-18 04:59:51

标签: php xml xpath nsxmlparser

我有xpath查询的输出,比如使用phhp代码 -

<?php

$variable=$_POST['module']; 
$xmldoc = new DOMDocument();
        $xmldoc->load('info.xml');

        $xpathvar = new Domxpath($xmldoc);

        $queryResult = $xpathvar->query("testcase[substring-after(
        substring-after(script, '/'),
        '/'
    ) = '$variable' or
    substring-before(
        substring-after(
            substring-after(script, '/'),
            '/'
        ),
        '/'
    ) = '$variable']"); 

foreach($queryResult as $var)
        {
                echo $var->textContent;
                echo "\n";

        }

?>

输出 -

Backup_Restore_04_Restore_mode_OFF_Restore 3630976 SDN_CONTROLLER scripts/testSuite/sdnSTC/Flare/Backup_Restore/Sprint16_tests/Backup_Restore_RestoreModeOFFRestore.tcl This test case is to verify Message on Restore mode OFF TCL STC OK 89765 NULL ALL SDN SDN_CONTROLLER target:system:family:stc-auto-workstation-B Backup_restore_05_restore_mode_on_No_Restore_file 3631050 SDN_CONTROLLER scripts/testSuite/sdnSTC/Flare/Backup_Restore/Sprint16_tests/Backup_Restore_RestoreModeOnNORestoreFile.tcl To verify Restore mode ON and there is no backupfile available for Restore and restart flare in normal mode TCL STC OK 89766 NULL ALL SDN SDN_CONTROLLER target:system:family:stc-auto-workstation-B 

我想要的是从这个输出中获取以.tcl结尾的所有字符串。

如何做到这一点。 请帮助..自从过去1周以来一直困在这里:(

2 个答案:

答案 0 :(得分:0)

尝试这种方法

foreach($queryResult as $var)
{
  echo $var->getElementsByTagName('script')->item(0)->textContent;
  echo "\n";
}

请更改

$queryResult = $xpathvar->query("testcase

$queryResult = $xpathvar->query("//testcase

答案 1 :(得分:0)

可以在数组中输出 -

foreach($queryResult as $var) { $array[] = basename($var->getElementsByTagName('script')->item(0)->textContent); }