You are here

class FeedsExXpathDomXpath in Feeds extensible parsers 7

Same name and namespace in other branches
  1. 7.2 src/FeedsExXml.inc \FeedsExXpathDomXpath

Wraps DOMXPath simplifying usage.

Hierarchy

Expanded class hierarchy of FeedsExXpathDomXpath

File

src/FeedsExXml.inc, line 366
Contains FeedsExXml.

View source
class FeedsExXpathDomXpath {

  /**
   * The XPath query object.
   *
   * @var DOMXPath
   */
  protected $xpath;

  /**
   * Constructs a FeedsExXpathDomXpath object.
   *
   * @param DOMDocument $document
   *   The DOM document to parse.
   *
   * @todo Add an option to force a deep scan of namespaces.
   */
  public function __construct(DOMDocument $document) {
    $this->xpath = new DOMXPath($document);

    // Find all namespaces.
    // Calling simplexml_import_dom() and SimpleXML::getNamespaces() is several
    // orders of magnitude faster than searching for the namespaces ourselves
    // using XPath.
    $simple = simplexml_import_dom($document);

    // An empty DOMDocument will make $simple NULL.
    if ($simple === NULL) {
      return;
    }
    foreach ($simple
      ->getNamespaces(TRUE) as $prefix => $namespace) {
      $this->xpath
        ->registerNamespace($prefix, $namespace);
    }
  }

  /**
   * Evaluates the XPath expression and returns a typed result if possible.
   *
   * @param string $expression
   *   The XPath expression to execute.
   * @param DOMNode $context_node
   *   (optional) The optional contextnode can be specified for doing relative
   *   XPath queries. Defaults to the root element.
   *
   * @see DOMXPath::evaluate()
   */
  public function evaluate($expression, DOMNode $context_node = NULL) {
    if ($context_node === NULL) {
      $context_node = $this->xpath->document;
    }
    return $this->xpath
      ->evaluate($expression, $context_node);
  }

  /**
   * Evaluates the given XPath expression.
   *
   * @param string $expression
   *   The XPath expression to execute.
   * @param DOMNode $context_node
   *   (optional) The optional contextnode can be specified for doing relative
   *   XPath queries. Defaults to the root element.
   *
   * @see DOMXPath::query()
   */
  public function query($expression, DOMNode $context_node = NULL) {
    if ($context_node === NULL) {
      $context_node = $this->xpath->document;
    }
    return $this->xpath
      ->query($expression, $context_node);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedsExXpathDomXpath::$xpath protected property The XPath query object.
FeedsExXpathDomXpath::evaluate public function Evaluates the XPath expression and returns a typed result if possible.
FeedsExXpathDomXpath::query public function Evaluates the given XPath expression.
FeedsExXpathDomXpath::__construct public function Constructs a FeedsExXpathDomXpath object.