Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[DomCrawler] Add support for XPath expression evaluation#19430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:masterfromjakzal:domcrawler-evaluate
Aug 2, 2016

Conversation

@jakzal
Copy link
Contributor

@jakzaljakzal commentedJul 26, 2016
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets#19162
LicenseMIT
Doc PRTODO

Example usage:

<?phpuseSymfony\Component\DomCrawler\Crawler;useSymfony\Component\VarDumper\VarDumper;require_once__DIR__.'/vendor/autoload.php';$html ='<html><body>    <span>Article 1</span>    <span>Article 2</span>    <span>Article 3</span></body></html>';$crawler =newCrawler();$crawler->addHtmlContent($html);VarDumper::dump($crawler->filterXPath('//span[contains(@id, "article-")]')->evaluate('substring-after(@id, "-")'));// array:3 [//   0 => "100"//   1 => "101"//   2 => "102"// ]VarDumper::dump($crawler->evaluate('substring-after(//span[contains(@id, "article-")]/@id, "-")'));// array:1 [//   0 => "100"// ]VarDumper::dump($crawler->filterXPath('//span[@class="article"]')->evaluate('count(@id)'));// array:3 [//   0 => 1.0//   1 => 1.0//   2 => 1.0// ]VarDumper::dump($crawler->evaluate('count(//span[@class="article"])'));// array:1 [//   0 => 3.0// ]VarDumper::dump($crawler->evaluate('//span[1]'));// Symfony\Component\DomCrawler\Crawler { }

mpyw reacted with thumbs up emojimpyw, aight8, and mickaelandrieu reacted with heart emoji
$data =array();
$prefixes =$this->findNamespacePrefixes($xpath);

foreach ($this->nodesas$node) {
Copy link
Member

@stofstofJul 26, 2016
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

you can use a single DOMXPath now. As of Symfony 3, it is impossible to have nodes belonging to different documents. They will all belong to$this->document (which can only benull for an empty root crawler, i.e. a crawler not yet initialized)

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thanks! PR updated.

publicfunctionevaluate($xpath)
{
$data =array();
$prefixes =$this->findNamespacePrefixes($xpath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I would throw a LogicException before that in case$this->document is null, saying you cannot evaluate an expression on an uninitialized crawler.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

👍 done

* this method will return either an array of simple types or a new Crawler instance.
*
* @param string $xpath An XPath expression
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

returning an array of a Crawler makes it difficult to work with this method in a fluent interface. This would be the first method returning a Crawler or something else.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yeah, I thought twice before defining the return type. I also don't like it but theDOMXPath::evaluate() returns a simple type value or DOMNodeList. The result depends on the type of xpath query. I figured that the end user knows what it's gonna be since he wrote the query.

Not sure how we could solve it differently (other than defining two methods and verifying results, or forbidding one type of queries).

@mpyw
Copy link

mpyw commentedAug 2, 2016

+1 👍

@fabpot
Copy link
Member

Thank you@jakzal.

@fabpotfabpot merged commit3148fad intosymfony:masterAug 2, 2016
fabpot added a commit that referenced this pull requestAug 2, 2016
…on (jakzal)This PR was merged into the 3.2-dev branch.Discussion----------[DomCrawler] Add support for XPath expression evaluation| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets |#19162| License       | MIT| Doc PR        | TODOExample usage:```php<?phpuse Symfony\Component\DomCrawler\Crawler;use Symfony\Component\VarDumper\VarDumper;require_once __DIR__.'/vendor/autoload.php';$html = '<html><body>    <span>Article 1</span>    <span>Article 2</span>    <span>Article 3</span></body></html>';$crawler = new Crawler();$crawler->addHtmlContent($html);VarDumper::dump($crawler->filterXPath('//span[contains(@id, "article-")]')->evaluate('substring-after(@id, "-")'));// array:3 [//   0 => "100"//   1 => "101"//   2 => "102"// ]VarDumper::dump($crawler->evaluate('substring-after(//span[contains(@id, "article-")]/@id, "-")'));// array:1 [//   0 => "100"// ]VarDumper::dump($crawler->filterXPath('//span[@Class="article"]')->evaluate('count(@id)'));// array:3 [//   0 => 1.0//   1 => 1.0//   2 => 1.0// ]VarDumper::dump($crawler->evaluate('count(//span[@Class="article"])'));// array:1 [//   0 => 3.0// ]VarDumper::dump($crawler->evaluate('//span[1]'));// Symfony\Component\DomCrawler\Crawler { }```Commits-------3148fad [DomCrawler] Add support for XPath expression evaluation
@jakzaljakzal deleted the domcrawler-evaluate branchAugust 4, 2016 06:35
@fabpotfabpot mentioned this pull requestOct 27, 2016
wouterj added a commit to symfony/symfony-docs that referenced this pull requestNov 10, 2016
This PR was merged into the master branch.Discussion----------[DomCrawler] Document XPath expression evaluationDocumentssymfony/symfony#19430Commits-------156b047 [DomCrawler] Document XPath expression evaluation
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

5 participants

@jakzal@mpyw@fabpot@stof@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp