{"id":219,"date":"2011-03-07T09:18:26","date_gmt":"2011-03-07T08:18:26","guid":{"rendered":"http:\/\/sematext.solr.pl\/?p=219"},"modified":"2020-11-11T09:18:56","modified_gmt":"2020-11-11T08:18:56","slug":"data-import-handler-xml-nested-entities","status":"publish","type":"post","link":"https:\/\/solr.pl\/en\/2011\/03\/07\/data-import-handler-xml-nested-entities\/","title":{"rendered":"Data Import Handler &#038; XML &#8211; nested entities"},"content":{"rendered":"<p>Data Import Handler is a very nice and powerful tool. The following entry is a description of the problem (and  solutions) which I met recently.<\/p>\n\n\n<!--more-->\n\n\n<h2>Description of the problem<\/h2>\n<p>I had to index some list of products, it doesn&#8217;t matter what kind of products.  However, the products can be combined into groups.  In  addition, every successive element in the group may have some data  omitted &#8211; actually the data that were present in the previous documents  that appeared in the group. Here is the example structure (irrelevant information was omitted for readability):\n<\/p>\n<pre class=\"brush:xml\">&lt;products&gt;\n  &lt;product&gt;\n    &lt;id&gt;1&lt;\/id&gt;\n    &lt;name&gt;Product 1&lt;\/name&gt;\n  &lt;\/product&gt;\n  &lt;product&gt;\n    &lt;id&gt;2&lt;\/id&gt;\n    &lt;name&gt;Product 2&lt;\/name&gt;\n  &lt;\/product&gt;\n  &lt;group&gt;\n    &lt;product&gt;\n      &lt;id&gt;3&lt;\/id&gt;\n      &lt;name&gt;Product 3 and 4&lt;\/name&gt;\n    &lt;\/product&gt;\n    &lt;product&gt;\n      &lt;id&gt;4&lt;\/id&gt;\n    &lt;\/product&gt;\n  &lt;\/group&gt;\n&lt;\/products&gt;<\/pre>\n<h2>Solution<\/h2>\n<p>The solution is as always a definition of the &#8220;entity&#8221; element which looked as follows:\n<\/p>\n<pre class=\"brush:xml\">&lt;entity processor=\"XPathEntityProcessor\"\n    forEach=\"\/products\/product | \/products\/group\/product\"&gt;\n  &lt;field column=\"id\" xpath=\"\/\/id\" \/&gt;\n  &lt;field column=\"name\" xpath=\"\/\/name\" commonField=\"true\" \/&gt;\n&lt;\/entity&gt;<\/pre>\n<h2>Explanation<\/h2>\n<p>With  this &#8220;forEach&#8221; design the processing will take place both for products  that do not belong to the group, as well as those in groups. An important attribute if the &#8220;commonField&#8221; one. It informs DIH that if the record doesn&#8217;t have a field defined DIH should fetch the field from the previous record.<\/p>\n<p>The above solution has some limitations, such as the first item in the group should have defined the field &#8220;name&#8221; and it is important to order the products, but in my case, those limitations corresponded exactly with the specifications of the provided import file.<\/p>","protected":false},"excerpt":{"rendered":"<p>Data Import Handler is a very nice and powerful tool. The following entry is a description of the problem (and solutions) which I met recently.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[27],"tags":[],"class_list":["post-219","post","type-post","status-publish","format-standard","hentry","category-solr-en"],"_links":{"self":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts\/219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/comments?post=219"}],"version-history":[{"count":1,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts\/219\/revisions"}],"predecessor-version":[{"id":220,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts\/219\/revisions\/220"}],"wp:attachment":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/media?parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/categories?post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/tags?post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}