Xpath 查询的输出 [英] Output of Xpath Query

查看:34
本文介绍了Xpath 查询的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先想问一下Xpath查询的输出格式是什么

First I would like to ask what is the format of the output of the Xpath query.

对我来说,当我查看输出的源代码时,我得到 view-source

For me when I do the view source of my output I get view-source

实际输出 - 输出

示例 xml - xmlfile

我的php代码-

<?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";
        }

?>

我想要的是得到以 .tcl 结尾的字符串而不是完整的内容

What I want is to get the string ending with .tcl instead of full content

请帮忙!!

推荐答案

首先你只是在寻找一个 testcase 元素节点,textcase 节点的实际路径是 /testcaseInfo/testcase 并且可以针对文档中的任何 testcase 元素进行优化:

First you're looking only for a testcase element node, the actual path to the textcase nodes is /testcaseInfo/testcase and can be optimized to any testcase element in the document:

//测试用例

接下来要验证元素是否以字符串结尾,您复制字符串的该部分并进行比较.

Next to validate if an element ends with a string you copy that part of the string and compare it.

//测试用例[子串(脚本,字符串长度(脚本) - 字符串长度('.tcl')+ 1) = '.tcl']

这将返回测试用例元素节点,现在您可以使用 detail 表达式来获取每个测试用例的数据:

This will return the testcase element nodes, now you can use detail expression to fetch the data for each testcase:

示例:

$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);

$expression = "
  //testcase[
    substring(
      script, 
      string-length(script) - string-length('$module') + 1
    ) = '$module'
  ]
";

// iterate test test cases and get name/script strings contents
foreach ($xpath->evaluate($expression) as $testCase) {
  var_dump(
    $xpath->evaluate('string(name)', $testCase),
    basename($xpath->evaluate('string(script)', $testCase))
  );
}     

输出:https://eval.in/120110

string(59) "802.1x_2.52_RADIUS_Accounting_AVP_contains_client_static_IP"
string(31) "802dot1xRadAccAVPClntStatIp.tcl"
string(27) "802.1x_1.02_Basic_User_Mode"
string(25) "802dot1xBasicUserMode.tcl"
string(44) "802.1x_2.47_RADIUS_Accounting_Enable_Disable"
string(34) "802dot1xRadiusAccEnableDisable.tcl"
string(31) "802.1x_2.14_RADIUS_Assigned_CoS"
string(29) "802dot1xRadiusAssignedCos.tcl"
...

这篇关于Xpath 查询的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆