首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于“子”值返回父节点的XPath

基于“子”值返回父节点的XPath
EN

Stack Overflow用户
提问于 2019-10-14 15:56:22
回答 1查看 25关注 0票数 1

是否可以只运行一个查询来替换多个查询?

代码语言:javascript
复制
 //host_info[hostname="localhost" and vulnerability_id="remote_execution"][1]
 //host_info[hostname="localhost" and vulnerability_id="remote_execution"][1]/@exclude
 //host_info[hostname="localhost" and vulnerability_id="remote_execution"][1]/../problem[1]
 //host_info[hostname="localhost" and vulnerability_id="remote_execution"][1]/../reference[1]
 //host_info[hostname="localhost" and vulnerability_id="remote_execution"][1]/../impact[1]
 //host_info[hostname="localhost" and vulnerability_id="remote_execution"][1]/../background[1]
 //host_info[hostname="localhost" and vulnerability_id="remote_execution"][1]/../resolution[1]

由于某些原因,这在PHP的xpath()函数中不起作用。

代码语言:javascript
复制
//host_info[hostname="localhost" and vulnerability_id="remote_execution"]/(*, ../background, ../impact, ../problem)

我觉得这里一定有更好的方法。请注意,有可能有多个<host_info>节点,这就是我将主机名和vulnerability_id作为目标的原因。但是父<background>节点中只有一个<resolution>节点,即<vulnerability_id>节点。

代码语言:javascript
复制
<report>
<vulnerability>
   <host_info>
      <hostname></hostname>
      <vulnerability_id></vulnerability_id>
   </host_info>
   <host_info>
      <hostname></hostname>
      <vulnerability_id></vulnerability_id>
   </host_info>
   <background></background>
   <resolution></resolution>
</vulnerability>
<vulnerability>
   <host_info></host_info>
   <host_info></host_info>
   <host_info></host_info>
   <host_info></host_info>
   <background></background>
   <resolution></resolution>
</vulnerability>
</report>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-14 16:38:23

节点列表只有一个维度,因此将多个项的详细信息序列化为单个列表并不真正有用。

通常,您需要使用一个Xpath表达式来标识列表节点,然后使用DOM方法和相关表达式来获取与这些节点相关的数据:

代码语言:javascript
复制
$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMxpath($document);

foreach ($xpath->evaluate('//host_info[hostname="localhost" and vulnerability_id="remote_execution"]') as $hostInfo) {
    var_dump(
      [
          $hostInfo->getAttribute('exclude'),
          $xpath->evaluate('string(parent::*/background)', $hostInfo),
          $xpath->evaluate('string(parent::*/resolution)', $hostInfo)
        ]
    );
}

对于单个项,可以将所有细节节点提取到单个结果列表中。然而,这样的表达式会很快变得复杂,然后您必须添加识别不同细节节点的逻辑。

代码语言:javascript
复制
$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMxpath($document);

$expression = <<<'XPATH'
//host_info[hostname="localhost" and vulnerability_id="remote_execution"][1]/@exclude|
(//host_info[hostname="localhost" and vulnerability_id="remote_execution"][1]/parent::*/background)[1]|
(//host_info[hostname="localhost" and vulnerability_id="remote_execution"][1]/parent::*/resolution)[1]
XPATH;

foreach ($xpath->evaluate($expression) as $detail) {
    var_dump(
       $detail->localName, $detail->textContent
    );
} 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58380236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档