Skip to main content
ocean waves

Blog Post

Can Views Use an External XML Source?

Ever want to use an external XML document to power your view?

Sometimes the need arises to include a list of upcoming events, recent news or job postings. The problem that sometimes occurs is the data exists on another site as an XML document. Now, you could use Feeds and periodically import those into your site as nodes and then have them set to expire (because you wouldn’t want to keep these forever). But that would take up space in your database and there’s no need for that level of complexity or trouble.

In my case, I wanted to create a view block on my site that included the recent security updates from Drupal.

I’ll give you the secret to doing it.

First go and install the Views XML Backend module. Make sure that your site meets the dependencies of the module listed.

Don’t forget to enable the module.

Next, you’ll want to go into the admin section and create a new view.

This is all too familiar, right? Create a name for it like “Recent Drupal Security Updates” and select “XML” as your View Settings.

Ah, something a little different there. Now, create a view block by selecting the "Create a block" checkbox under Block Settings.

Hit “Save and Edit” on the new the view.

Next, expand the Advanced settings and click on "Query settings."

Enter your RSS Feed's XML link into the "XML File" field.

If it's an RSS Feed, the proper Row XPath function should look like "//item"; note your eyes are not deceiving you, this does indeed start with two slashes. That’s an XPath thing and it must be formatted like that, otherwise it won’t work.

If you’re not using an RSS Feed, you may want to investigate the topic XPath.

If you want to read more about XPath, I recommend the following two resources:

  • https://developer.mozilla.org/en-US/docs/Web/XPath
  • https://www.w3.org/TR/xpath-31/

Let’s go and add some fields to the display starting with the link. Click on “Add” next to the field display. You’ll notice the new XML options. Add a new "XML Text" field for the link.

Configure the link field by adding the XPath selector. I changed the field to be excluded from display because I want to use the link field value as a token on the title to make that a link to the resource on Drupal.org.

Next up, creating the title field. Go through the same process you did for the link field. Configure the title field so that it links directly to the security update by expanding the “Rewrite Results” settings. You may need to click on the complete token list to see which field you want to grab data from. In my case, I wanted “{{ text_1 }}”, which refers to the link field we just created.

Follow the steps in the previous two field additions to now create a description field. We’re limited on space in my view block so configure the description field to only show the first 100 characters. Make sure you strip out the html tags and add an ellipsis to indicate that there is more information.

For the sake of completeness, I added a Sort Criteria field but I think that in my case it may not be needed. I used the pubDate element in the XML to sort by in date descending order, so the newest information will be shown at the top.

After saving the view, go to the block administration page to place the new block. Scroll down to the disabled blocks section and click on the Place Block button. Selected the new view block titled "Recent Drupal Security Updates" by clicking on the Place Block button.

In my case, I only want to show this important information on the home page. Configure the block's visibility setting to be “<front>”. Finally, select the region you want the block to display in and hit “Save block”.

Now when I visit the homepage, I see the new view block with the recent security updates and it's using external XML as my datasource. Woot!

There are many places online that offer XML data as a source so you can use it on your site. The possibilities are endless. Please feel free to tell us about your experiences with this tutorial in the comments below. Good luck!

Additional Resources
20 Things You Must Know Before Approaching A Web Agency | Blog Post
Evaluating the Security of Drupal Contrib Modules | Blog Post
Ultimate Markup Control in Views | Blog Post