I am trying to populate an e-mail with 5 images and text using ampscript and a RSS-feed.

The RSS looks like this:

<enclosure height="638" length="123028" type="image/jpeg" url="imageurl.jpg" width="960"/>
<category>bar category</category>

The "enclosure"-node is the source for the images is not always present, so I am trying to figure out a way to skip that item in the for-loop.

Here is my amscript (trimmed down to the basics):


var @xml
set @xml = httpget(xmlfeed.rss)
set @test = BuildRowsetFromXML(@xml,"//item/enclosure",0)

var @i, @maxToDisplay, @nodes, @test, @rows, @rowCount

set @test = BuildRowsetFromXML(@xml,"//item/enclosure",0)
set @nodes = BuildRowsetFromXML(@xml,"//item/*",0)
set @maxToDisplay = 5

If RowCount(@test) >= @maxToDisplay THEN
    set @rowCount = @maxToDisplay 
    SET @rowCount = RowCount(@test) 

for @i = 1 to @rowCount do 

   set @imageurl = Field(Row(BuildRowsetFromXML(@xml,concat("//item[",@i,"]/enclosure"),0),1),'url_att',0) 


When the enclosure-node is not present I get the following error-message:

Invalid index (parameter 2) passed to Row function. Index must be less than or equal to the row count. Index Value: 1 Row Count: 0

I've tried reviewing the ampscript documentation for all functions, which is why the Field-function has a second parameter of 0, which should return a null value on error. However, this is not the case here.

I hope someone can help me figure out how to get ampscript to return a null value, if the node is not present. Ultimately, I am just interested in skipping an item, if the enclosure-node is not present.

Thanks, Emil


Since you're dealing with rowsets, you'll need to utilize rowcount() to check for results.

Here's my go-to boilerplate for parsing XML. The conditionals after the @nodepath assignment is the part pertinent to your question:


var @xml, @isXML, @nodes, @rowCount
set @xml = AttributeValue("xml")

if indexOf(@xml,"<cart>") > 0 then

  set @nodes = BuildRowsetFromXML(@xml,"/cart/items/item",0)
  set @rowCount = rowcount(@nodes)

  if @rowCount > 0 then

    for @i = 1 to @rowCount do

      var @nodepath 
      var @sku
      var @name
      var @url

      set @nodepath = concat("/cart/items/item[",@i,"]/")

      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"sku"))) > 0 then
          set @sku = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"sku"),0),1),'Value')

      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"name"))) > 0 then
          set @name = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"name"),0),1),'Value')

      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"url"))) > 0 then
          set @url = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"url"),0),1),'Value')

      if not empty(@sku) and not empty(@name) and not empty(@url) then


       <br><a href="%%=redirectto(@url)=%%">%%=v(@sku)=%%</a> - <a href="%%=redirectto(@url)=%%">%%=v(@name)=%%</a>



    next @i


   outputline(concat("<br>no products found"))



  outputline(concat("<br>no XML found"))


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.