You are here

public function MigrateXMLReader::__construct in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 plugins/sources/xml.inc \MigrateXMLReader::__construct()

Prepares our extensions to the XMLReader object.

Parameters

string $xml_url: URL of the XML file to be parsed.

string $element_query: Query string in a restricted xpath format, for selecting elements to be

string $id_query: Query string to the unique identifier for an element, relative to the root of that element. This supports the full xpath syntax.

File

plugins/sources/xml.inc, line 964
Support for migration from XML sources.

Class

MigrateXMLReader
Makes an XMLReader object iterable, returning elements matching a restricted xpath-like syntax.

Code

public function __construct($xml_url, $element_query, $id_query) {
  $this->reader = new XMLReader();
  $this->url = $xml_url;
  $this->elementQuery = $element_query;
  $this->idQuery = $id_query;

  // Suppress errors during parsing, so we can pick them up after.
  libxml_use_internal_errors(TRUE);

  // Parse the element query. First capture group is the element path, second
  // (if present) is the attribute.
  preg_match_all('|^/([^\\[]+)(.*)$|', $element_query, $matches);
  $element_path = $matches[1][0];
  $this->elementsToMatch = explode('/', $element_path);
  $attribute_query = $matches[2][0];
  if ($attribute_query) {

    // Matches [@attribute="value"] (with either single- or double-quotes).
    preg_match_all('|^\\[@([^=]+)=[\'"](.*)[\'"]\\]$|', $attribute_query, $matches);
    $this->attributeName = $matches[1][0];
    $this->attributeValue = $matches[2][0];
  }

  // If the element path contains any colons, it must be specifying
  // namespaces, so we need to compare using the prefixed element
  // name in next().
  if (strpos($element_path, ':')) {
    $this->prefixedName = TRUE;
  }
}