I have an XML with about the following abridged structure:

<document>
<foo>
<item>
<url href="https://drupal.org" type="Important"/>
</item>
<item>
</item>
<item>
</item>
<item>
</item>
</foo>
<bar>
<item>
</item>
<item>
<url href="https://wordpress.org" type="Unportant"/>
</item>
<item>
</item>
</bar>
</document>

I am migrating foo and bar items into different node types. Since the URLs have more info than I can fit into a Link-Field I either want to make them field collections or also their own node type or custom entity.

I guess I can do so by making separate migrations foo_url and bar_url but since there are also also node types, this would be a bit unwieldy.

So, what I am asking for is an item_selector that can have more than one xpath.

Comments

killes@www.drop.org created an issue. See original summary.

killes@www.drop.org’s picture

NB: I've tried to specify the item_selector as "//url" (and "///url" as one / seemed to get stripped) but the search code does not seem to like this.

killes@www.drop.org’s picture

<document>
<foo>
<item id="789">
<url href="https://drupal.org" type="Important"/>
</item>
<item>
</item>
<item>
</item>
<item>
</item>
</foo>
<bar>
<item>
</item>
<item id="1243">
<url href="https://wordpress.org" type="Unportant"/>
</item>
<item>
</item>
</bar>
</document>

Another problem has arisen:

I need to be able to get the parent id in order to save a reference to it on the url entity.

This currently doesn't work as the parent isn't available when matching.

killes@www.drop.org’s picture

Actually, I am wondering why the matching process is set up as it is. Ie that first is gets the items from the item selector and then the fields using the field selectors. Is that a performance consideration?

killes@www.drop.org’s picture

Status: Active » Needs review
StatusFileSize
new9.78 KB

So, since I didn't understand the parser, I rewrote it.

Bonusses:
- much less code
- you can use full xpath expressions in the item selector, ie /foo/*/bar
- you can now use full xpath expressions in the field declararions (../@id)

Malusses:
- potentially more memory usage, although I didn't test it.

Changes:
- the item_selector now needs to contain the namespace declarations
- the field selectors now no longer need to contain the namespace declarations

killes@www.drop.org’s picture

StatusFileSize
new9.78 KB

Ah, sorry, the previous version lacked a return TRUE statement.

mikeryan’s picture

Component: API » Plugins
Status: Needs review » Needs work

A bit of history - in D7 migrate, the original XML source plugin used simplexml. This ran into trouble, though, with very large XML sources (like, 200MB WordPress dumps), because simplexml parses the entire file into memory in one go. So, an alternative XML source plugin was implemented (see https://www.drupal.org/node/1152156) using XMLReader as a basis, which permits incremental parsing of an XML file, with the disadvantage being that without the whole thing in memory you cannot perform xpaths across the entire source.

When it came to D8, the original XML source plugin work was done by mbaynton, who was working on WordPress imports and thus ported the XMLReader approach. That has been incorporated into the XML parser plugin in migrate_plus - it has always been my intention to add a simplexml variant, but that hasn't been a priority. Looks like you're on it, though, which is great - but please don't take away the XMLReader version! Could you reroll the simplexml approach as a new parser plugin?

Thanks.

killes@www.drop.org’s picture

I was thinking it might have to do with performance. :)

Yes, I'll create a separate plugin.

If you (or anybody else) does have to share information on performance considerations doing migrations, I am very interested in hearing them.

mikeryan’s picture

In terms of performance, much of https://www.drupal.org/node/2136603 still applies for D8.

mikeryan’s picture

Title: XML: multiple sub-items, item_selector as array » Provide alternative SimpleXML-based parser

Retitling to reflect the current direction.

Note that the last patch uses simplexml_load_file() - using simplexml_load_string() instead would enable us to make proper use of the fetcher plugins (which XMLReader is unable to do, because the fetching is thoroughly embedded).

amateescu’s picture

Status: Needs work » Needs review
StatusFileSize
new5.96 KB

Here's the new SimpeXML parser implemented as a separate plugin.

I haven't addressed #10 yet because I'm not sure if it needs more than just replacing simplexml_load_file() with simplexml_load_string().

mikeryan’s picture

Status: Needs review » Needs work

I haven't addressed #10 yet because I'm not sure if it needs more than just replacing simplexml_load_file() with simplexml_load_string().

It does require just a bit more, the simple_xml source plugin needs to invoke the fetcher to retrieve the XML content. This is simple enough - look at the JSON parser's getSourceData():

    $response = $this->getDataFetcherPlugin()->getResponseContent($url);
    // Convert objects to associative arrays.
    $source_data = json_decode($response, TRUE);

So, just pass the response to simplxml_load_string() instead of json_decode()...

I'm thinking now maybe, rather than a separate XML parser plugin, instead we had the one plugin with an option indicating which mechanism (XMLReader or SimpleXML) to use for the XML source. It would make the plugin code more complex, but I think it would be better DX. Thoughts?

joshi.rohit100’s picture

Just a random thought - can we parse/convert nodes (getting from xpath) into array and just use the array iterator as we have in json ?

mikeryan’s picture

can we parse/convert nodes (getting from xpath) into array and just use the array iterator as we have in json ?

Since json_decode($json, TRUE) returns an array, use of the array iterator comes naturally there. I'm don't see what advantage we might gain by converting XML to an array up front, and we would lose the full power of xpaths (e.g., to retrieve attributes).

As it happens, I need this for a current project, so I will pick up with using the fetcher, and ideally setting things up so we can have a single xml parser plugin which can use either the XMLReader or SimpleXML approach.

mikeryan’s picture

StatusFileSize
new6.08 KB
new606 bytes

Here's what I was talking about - this version will make use of the configured fetcher plugin to retrieve the data.

Needs work because I want to clean this up some - either see if we can have a single xml parser plugin where you can select the XMLReader or SimpleXML approach, or if that doesn't come out clean enough, at least move the shared bits into a trait rather than have one extend the other.

mikeryan’s picture

Status: Needs work » Needs review
StatusFileSize
new6.8 KB
new4.78 KB

Going with the trait.

damontgomery’s picture

This patch was tested on our project and works. We are importing an XML feed over SFTP. The SFTP pattern is set for the `urls` field like this, `ssh2.sftp://demo:password@example.com:22/myfolder/myfile.xml`. We set the `data_fetcher_plugin` to `file` and the `data_parser_plugin` to `simple_xml`. This requires libssh2 to be installed with the ssh2 php extension.

See http://php.net/manual/en/book.ssh2.php for more information about ssh2.

The background for this combination of modules is that XMLReader (xml) only allows reading XML files from a URL. the pattern above uses php file_get_contents() to call the SFTP library, so we need to read from a file.

damontgomery’s picture

Status: Needs review » Reviewed & tested by the community

mikeryan’s picture

Status: Reviewed & tested by the community » Fixed

Committed, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.