XPath 同级 returns 为空
XPath Sibling returns empty
我正在使用这个 http://www.xpathtester.com/xpath/5a30592045b6aa5089faf909261ede0b XPath 测试器,它 returns 正是我想要的。出于某种原因,它删除了我的完整查询,但如果你使用它,它就会起作用。
*/h3[contains(string(), "Description")]/following-sibling::p[1]
但在现实生活中,我从变量中什么也得不到。
我正在尝试获取 <h3>Description</h3>
之后的数据,在本例中是一段 <p>
.
HTML
$feed_item=
<div class="outer-feed"><ul>
<li><strong>Severity:</strong> <span class="label label-info">Low</span></li>
</ul>
<h3>Description</h3>
<p>The lack of validation of configuration parameters used in SQL queries caused various SQL injection vectors.</p>
...
这是我的 XPath
$description_node = $xpath->query('*/h3[contains(string(), "Description")]/following-sibling::p[1]', $feed_item);
$description = "description: " . $description_node->item(0)->textContent;
和var_dump
object(DOMNodeList)#1654 (1) { ["length"]=> int(0) }
和错误
Notice
: Trying to get property 'textContent' of non-object in
让我感到困惑的是,我可以通过使用以下命令从同一个 HTML 获得严重性:
$severity_node = $xpath->query('*/li[contains(string(), "Severity:")]', $feed_item);
$severity = preg_replace('/Severity:\W*/u', '', $severity_node->item(0)->textContent);
我的第一个想法是缩小到 H3 并输出它。
$description_node = $xpath->query('*/h3[contains(string(), "Description")]', $feed_item);
object(DOMNodeList)#1654 (1) { ["length"]=> int(0) } // doesn't contain anything
鉴于以下内容相同,但第一个有效而第二个无效,可能是什么问题?
$severity_node = $xpath->query('*/li[contains(string(), "Severity:")]', $feed_item);
$description_node = $xpath->query('*/h3[contains(string(), "Description")]', $feed_item);
为什么一个工作而另一个不工作。解决此类问题的最佳方法是什么。它似乎适用于 xpathtester。在 PHP 中我可能做错了什么导致这个问题?
仅当调用时的上下文项是 h3
元素的祖父项时,以 */h3[...]
开始的查询才有效。您没有提供有关上下文项的任何信息,因此我怀疑它有所不同。
您提出的问题:"Why is one working and not the other. And what is the best way to troubleshoot things like this. It seems to work on the xpathtester. What could I be doing wrong that causes this problem in PHP?"
嗯,首先要了解 XPath 表达式可能取决于上下文项,并且使用不同的上下文项评估的相同表达式将给出不同的结果。一旦你理解了这个概念,它就会变得更加清晰。
试试这个 XPath:
//h3[text()="Description"]/following::p[1]
我正在使用这个 http://www.xpathtester.com/xpath/5a30592045b6aa5089faf909261ede0b XPath 测试器,它 returns 正是我想要的。出于某种原因,它删除了我的完整查询,但如果你使用它,它就会起作用。
*/h3[contains(string(), "Description")]/following-sibling::p[1]
但在现实生活中,我从变量中什么也得不到。
我正在尝试获取 <h3>Description</h3>
之后的数据,在本例中是一段 <p>
.
HTML $feed_item=
<div class="outer-feed"><ul>
<li><strong>Severity:</strong> <span class="label label-info">Low</span></li>
</ul>
<h3>Description</h3>
<p>The lack of validation of configuration parameters used in SQL queries caused various SQL injection vectors.</p>
...
这是我的 XPath
$description_node = $xpath->query('*/h3[contains(string(), "Description")]/following-sibling::p[1]', $feed_item);
$description = "description: " . $description_node->item(0)->textContent;
和var_dump
object(DOMNodeList)#1654 (1) { ["length"]=> int(0) }
和错误
Notice
: Trying to get property 'textContent' of non-object in
让我感到困惑的是,我可以通过使用以下命令从同一个 HTML 获得严重性:
$severity_node = $xpath->query('*/li[contains(string(), "Severity:")]', $feed_item);
$severity = preg_replace('/Severity:\W*/u', '', $severity_node->item(0)->textContent);
我的第一个想法是缩小到 H3 并输出它。
$description_node = $xpath->query('*/h3[contains(string(), "Description")]', $feed_item);
object(DOMNodeList)#1654 (1) { ["length"]=> int(0) } // doesn't contain anything
鉴于以下内容相同,但第一个有效而第二个无效,可能是什么问题?
$severity_node = $xpath->query('*/li[contains(string(), "Severity:")]', $feed_item);
$description_node = $xpath->query('*/h3[contains(string(), "Description")]', $feed_item);
为什么一个工作而另一个不工作。解决此类问题的最佳方法是什么。它似乎适用于 xpathtester。在 PHP 中我可能做错了什么导致这个问题?
仅当调用时的上下文项是 h3
元素的祖父项时,以 */h3[...]
开始的查询才有效。您没有提供有关上下文项的任何信息,因此我怀疑它有所不同。
您提出的问题:"Why is one working and not the other. And what is the best way to troubleshoot things like this. It seems to work on the xpathtester. What could I be doing wrong that causes this problem in PHP?"
嗯,首先要了解 XPath 表达式可能取决于上下文项,并且使用不同的上下文项评估的相同表达式将给出不同的结果。一旦你理解了这个概念,它就会变得更加清晰。
试试这个 XPath:
//h3[text()="Description"]/following::p[1]