Problem/Motivation

In the context of dom process plugins, the general idea is to manipulate html before storing it into Drupal.
One possible use case is to extract information from a given part of the markup, and ignore the rest.

Last year at NEDCamp I suggested to write something to do this as someone was telling me his use case.
Lately I found myself needing something like this.

A nice related note: the current dom migrate process plugins inside migrate plus are more generic than html processing, since it is handling a \DOMDocument object, which can also be generated from other sources like xml; so it is useful not only for html processing.

Proposed resolution

Create a new migrate process plugin that can receive a \DOMDocument, e.g. generated with the dom plugin, and given some options extract a subset of the data and return it as a simple string.

Examples

A couple of examples.

process:
  'body/value':
    -
      plugin: dom
      method: import
      source: 'body/0/value'
    -
      plugin: dom_extract
      xpath: '/div/div'

This will extract the contents of the second nested <div> element.

process:
  field_data_type:
    -
      plugin: dom
      method: import
      source: 'body/0/value'
    -
      plugin: dom_extract
      xpath: '/h1'
      target: 'attribute:data-type'

This will extract the contents of the data-type attribute on the top level h1 element.

Remaining tasks

Provide patch.
Review.

User interface changes

N.A.

API changes

New dom_extract migrate process plugin.

Data model changes

N.A.

Release notes snippet

New dom_extract migrate process plugin to retrieve a subset of a given dom as a string.

CommentFileSizeAuthor
#4 3112559-4.patch1.58 KBbenjifisher
#3 3112559-2.patch4.35 KBmarvil07

Comments

marvil07 created an issue. See original summary.

marvil07’s picture

Title: Extract a given xpath » New process plugin to extract a given xpath from a DOMDocument
Assigned: marvil07 » Unassigned
Issue summary: View changes
Status: Active » Needs review
Issue tags: -Extract a given xpath

Here the first patch.

marvil07’s picture

StatusFileSize
new4.35 KB

OK, really upload the patch.

benjifisher’s picture

StatusFileSize
new1.58 KB

On the DrupalSpoons version of this issue, I just added another way to provide this functionality. For convenience, I am attaching a patch to provide the dom_select plugin.

See the doc block in the patch for usage information. See the DrupalSpoons issue for a comparison of the two plugins.

heddn’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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