The Mask
The Mask

Reputation: 17427

How use xpath queries in PHPQuery?

I'm looking for an example code how use xpath phpQuery. I read the wiki-pages but not found any thing. thanks in advance.

Upvotes: 2

Views: 1072

Answers (2)

Danora
Danora

Reputation: 345

So PHPQuery actually has a function called protected function getNodeXpath($oneNode = null, $namespace = null). You can find it pretty easily if you just look in the phpquery_onefile.

If you simply change this protected function to public, you can now use it in your code. You would just need to do something like this: pq('button')->getNodeXpath().

Keep in mind though, this returns an array, and a lot of functions (such as those in php-webdriver) will require a string xpath. To fix this, simply implode the array you get back.

Upvotes: 1

Phil
Phil

Reputation: 164912

The project summary states

phpQuery is a server-side, chainable, CSS3 selector driven Document Object Model (DOM) API based on jQuery JavaScript Library.

As XPath is not part of CSS3 selectors and there are no references in the documentation, I'd say it hasn't been implemented.

Update

From digging through their source, it looks like it wraps DOMDocument and you can retrieve the documents from the phpQuery::$documents array. Once you have the DOMDocument instance, you can perform create a DOMXPath object and perform queries on it.

Upvotes: 0

Related Questions