You are here

class XpathDomXpath in Feeds extensible parsers 8

Wraps DOMXPath simplifying usage.

Hierarchy

Expanded class hierarchy of XpathDomXpath

1 file declares its use of XpathDomXpath
XmlParser.php in src/Feeds/Parser/XmlParser.php

File

src/XpathDomXpath.php, line 12

Namespace

Drupal\feeds_ex
View source
class XpathDomXpath {

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

  /**
   * Constructs a XpathDomXpath 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
XpathDomXpath::$xpath protected property The XPath query object.
XpathDomXpath::evaluate public function Evaluates the XPath expression and returns a typed result if possible.
XpathDomXpath::query public function Evaluates the given XPath expression.
XpathDomXpath::__construct public function Constructs a XpathDomXpath object.