I have an XML file with projects and tasks. Every project can have tasks and subprojects of arbitrary depth. I have read the documentation about XML-based sources at https://drupal.org/node/1152156 and the XML examples in the module, but they just seem to deal with flat, one-level files easily accessible with fixed XPath paths.

My file has a structure similar to this:

<data>
  <project>
    <id>...</id>
    ...
    <task>
      <id>...</id>
      ...
    </task>
    <project>
      <id>...</id>
      ...
      <task>
        <id>...</id>
        ...
      </task>
      <project>
        ...
      </project>
    </project>
  </project>
</data>

How can I migrate this file into nodes of type "project" and "task"? The question is not how to create that hierarchy with nodes in Drupal, but how to traverse this file with migrate.

Comments

Ronino’s picture

Issue summary: View changes
13rac1’s picture

Status: Active » Postponed (maintainer needs more info)

I'd suggest you pre-process all your data into a separate bare MySQL database. It'll make the migration far simpler.

OR

Create two XML migrations using the same source. Assuming you have an entity_reference pointing from the task to the project. You make all the projects first. Then make all the tasks referencing the projects.

mikeryan’s picture

The answer depends how you're implementing the hierarchy in Drupal. Let's say that each project has a node reference field pointing to its parent project, field_parent_project (NULL, of course, for top-level projects). Then, you should be able to handle the relationship with something like (bearing with my xpath rustiness):

$this->addFieldMapping('field_parent_project', 'parent_id')
     ->xpath('../project/id')
     ->sourceMigration($this->machine_name);

This would pull the parent's source ID, and (since the parent would be saved before its children) translating that to the parent's Drupal node ID using your migration's map table.

mikeryan’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)
Alina Basarabeanu’s picture

Hi,
I've got same problem, can't get the parent id.
My xml structure is :

<feed>
  <itemList>
    <item>
      <summary>
        <itemID>1793006</itemID>
      </summary>
      <childrenList>
        <child>
          <base>5605</base>
        <total>5605</total>
        </child>
      <child>
        <base>5605</base>
      <total>5605</total>
      </child>
    </childrenList>
    </item>
    <item>
      <summary>
        <itemID>1793006</itemID>
      </summary>
      <childrenList>
        <child>
          <base>5605</base>
          <total>5605</total>
        </child>
        <child>
          <base>5605</base>
          <total>5605</total>
        </child>
      </childrenList>
    </item>
  </itemList>
</feed>

I run one migrate which import each item.
The problem in on second migration where I am using :

$item_xpath = '/feed/itemList/item/childrenList/child';
$item_id_xpath = 'childID';
$this->source = new MigrateSourceXML($items_url, $item_xpath, $item_id_xpath, $source_fields);

childID is created in prepareKey(), doesn't exist in xml.
The xml for each child doesn't contain any information about the parent item and I need the itemID.
Already tried to use a field on child entity but still can't get the parent id.