[英]Symfony DomCrawler with XML and XPath
I'm trying to get all the title
elements and save them in an array. 我正在尝试获取所有
title
元素并将它们保存在数组中。
XML: XML:
<?xml version="1.0" encoding="UTF-8"?>
<mylist>
<element>
<id>1</id>
<title>Example 1</title>
<status>2</status>
<my_status>2</my_status>
</element>
<element>
<id>2</id>
<title>Example 2</title>
<status>1</status>
<my_status>1</my_status>
</element>
<element>
<id>3</id>
<title>Example 3</title>
<status>2</status>
<my_status>6</my_status>
</element>
<element>
<id>4</id>
<title>Example 4</title>
<status>3</status>
<my_status>6</my_status>
</element>
<element>
<id>5</id>
<title>Example 5</title>
<status>1</status>
<my_status>6</my_status>
</element>
</mylist>
PHP: PHP:
$crawler = new Crawler();
$crawler->addXmlContent($data);
$result = $crawler->filterXPath('/mylist/element[not(status=3) and my_status=6]/title/text()');
The elements nodes needs to satisfy some conditions, so calling $result->count()
should print 2
(Example 3 & Example 5), but it prints 0. 元素节点需要满足一些条件,因此调用
$result->count()
应该显示2
(示例3和示例5),但它显示0。
Thanks. 谢谢。
EDIT: 编辑:
Found the solution, the XPath should be: 找到解决方案后,XPath应该是:
$result = $crawler->filterXPath('//mylist/element[not(status=3) and my_status=6]/title/text()');
From filteXpath annotation 来自filteXpath批注
* The XPath expression is evaluated in the context of the crawler, which
* is considered as a fake parent of the elements inside it.
* This means that a child selector "div" or "./div" will match only
* the div elements of the current crawler, not their children.
Then inside method $xpath = $this->relativize($xpath);
然后在方法
$xpath = $this->relativize($xpath);
is applied where your path is modified. 适用于修改路径的位置。
For me most simple solution was just to use relative path like ./mylist
. 对我而言,最简单的解决方案是使用
./mylist
之类的相对路径。
But if you can understand what's going on here https://github.com/symfony/dom-crawler/blob/master/Crawler.php#L958 absolute path should be possible, I think 但是,如果您能了解这里发生了什么,我认为绝对路径应该可行https://github.com/symfony/dom-crawler/blob/master/Crawler.php#L958
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.