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
Comment #2
killes@www.drop.org commentedNB: 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.
Comment #3
killes@www.drop.org commentedAnother 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.
Comment #4
killes@www.drop.org commentedActually, 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?
Comment #5
killes@www.drop.org commentedSo, 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
Comment #6
killes@www.drop.org commentedAh, sorry, the previous version lacked a return TRUE statement.
Comment #7
mikeryanA 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.
Comment #8
killes@www.drop.org commentedI 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.
Comment #9
mikeryanIn terms of performance, much of https://www.drupal.org/node/2136603 still applies for D8.
Comment #10
mikeryanRetitling 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).
Comment #11
amateescu commentedHere'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()withsimplexml_load_string().Comment #12
mikeryanIt 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():
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?
Comment #13
joshi.rohit100Just a random thought - can we parse/convert nodes (getting from xpath) into array and just use the array iterator as we have in json ?
Comment #14
mikeryanSince 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.
Comment #15
mikeryanHere'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.
Comment #16
mikeryanGoing with the trait.
Comment #17
damontgomery commentedThis 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.
Comment #18
damontgomery commentedComment #20
mikeryanCommitted, thanks!