Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

How to insert a fixed string value in a feeds feed with xpath parser

Parent Feed: 

So you have a feed that you aggregate through feeds, and all new items should have the same value in a given field. For example if you have feeds from 4 different webshops, you may want to add a fixed value of the webshop description that is not present in the feed for each different feed. So how do you make a feeds feed give the same value for each feed item? If you use feeds x path parser (as I often do), how do you fix a value for each item in the feed? Or more specifically, how do you insert a fixed value with xpath?

Given that you already are familiar with xpath and feeds, the best answer I have found is an xpath function called concat. Concat combines two or more values into one, I.e concat(“animated “,”gifs”,” or GTFO”) will give the output “animated gifs or GTFO”.

As you already may have figured out, this can be used both to fix a string value, or as a prefix to your feed item value in feeds. I.e concat(“my guid in this feed item is “,”$guid”) will give something like “my guid in this feed item is 1234guid_uniqueID”.

Note that concat needs at least two arguments. So if your fixed value is supposed to be “animated_gifs” with no spaces, use concat(“animated”,”_gifs”) or similar.

Again, this worked for me (as of dev version august 2011 of feeds xpath parser – not alpha 4). If you have a simpler solution, please share it in the comment section.

Let's make a theoretical example: say you want a fixed field with a file reference, that always renders in an <img> tag. concat(“blues04”,”.gif”) would give this output:

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web