Symfony\Component\DomCrawler\Crawler::filterXPath PHP Method

filterXPath() public method

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.
public filterXPath ( string $xpath ) : Crawler
$xpath string An XPath expression
return Crawler A new instance of Crawler with the filtered list of nodes
    public function filterXPath($xpath)
    {
        $xpath = $this->relativize($xpath);
        // If we dropped all expressions in the XPath while preparing it, there would be no match
        if ('' === $xpath) {
            return $this->createSubCrawler(null);
        }
        return $this->filterRelativeXPath($xpath);
    }

Usage Example

コード例 #1
0
 /**
  * Returns a method in the current specification from a DOMNode
  *
  * @param \DOMNode $node A DOMNode
  *
  * @return Method
  */
 public function getMethod(\DOMNode $node)
 {
     $crawler = new Crawler($node);
     $name = $crawler->attr('name');
     // Initialize
     $method = new Method($name);
     // Type
     $method->setType(preg_match('/(^(get|is)|ToString$)/', $name) ? Method::TYPE_ACCESSOR : Method::TYPE_ACTION);
     // Description
     $descriptions = $crawler->filterXPath('//comment');
     if (count($descriptions) !== 1) {
         throw new \Exception('Only one comment expected');
     }
     $descriptions->rewind();
     $description = $this->getInner($descriptions->current());
     $method->setDescription($description);
     // Parameters
     foreach ($crawler->filterXPath('//parameter') as $node) {
         $method->addParameter($this->getParameter($node));
     }
     // Return
     $returnNodes = $crawler->filterXPath('//return');
     if (count($returnNodes) > 1) {
         throw new \Exception("Should not be more than one return node");
     } elseif (count($returnNodes) == 1) {
         $returnNodes->rewind();
         list($type, $description) = $this->getReturn($returnNodes->current());
         $method->setReturnType($type);
         $method->setReturnDescription($description);
     }
     return $method;
 }
All Usage Examples Of Symfony\Component\DomCrawler\Crawler::filterXPath