You are here:  » Extract XML Duplicates Filter

Active Forum Topics


Extract XML Duplicates Filter

Submitted by support on Thu, 2018-04-12 13:17 in

Hi everyone,

Something that comes up relatively often in XML format feeds is duplicated element names for example;

<features>
  <feature>Feature 1</feature>
  <feature>Feature 2</feature>
  <feature>Feature 3</feature>
</features>

The parser resolves duplicates by appending @1, @2 etc. to repeated element names, so for the above example, you would see the following field names in the Sample Data on Feed Registration Step 2:

FEATURES/FEATURE
FEATURES/FEATURE@1
FEATURES/FEATURE@2

The following filter will enable these to be extracted into a single, comma separated value:

  /*************************************************/
  /* extractXMLDuplicates */
  /*************************************************/
  $filter_names["extractXMLDuplicates"] = "Extract XML Duplicates";
  function filter_extractXMLDuplicatesConfigure($filter_data)
  {
    widget_textBox("Base Field Name","field",TRUE,$filter_data["field"],"",2);
  }
  function filter_extractXMLDuplicatesValidate($filter_data)
  {
    if (!$filter_data["field"])
    {
      widget_errorSet("field","required field");
    }
  }
  function filter_extractXMLDuplicatesExec($filter_data,$text)
  {
    global $filter_record;
    $items = array();
    $i = 0;
    $p = "";
    while(1)
    {
      if ($i) $p = "@".$i;
      $field = $filter_data["field"].$p;
      if (!isset($filter_record[$field])) break;
      $items[] = $filter_record[$field];
      $i++;
    }
    return implode(",",$items);
  }

With the above code added to includes/filter.php, and continuing the FEATURES/FEATURE example, to populate a custom field "features" simply add a new Extract XML Duplicates filter to the Features field, with the Base Field Name configured as follows:

FEATURES/FEATURE

After import, wherever a $product or ($main_product) array is in context, the "features" key will contain "Feature 1,Feature 2,Feature 3".

Cheers,
David
--
PriceTapestry.com