I am trying to populate an e-mail with 5 images and text using ampscript and a RSS-feed.
The RSS looks like this:
<title>title</title>
<link>foo.com</link>
<enclosure height="638" length="123028" type="image/jpeg" url="imageurl.jpg" width="960"/>
<category>bar category</category>
</item>
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
ELSE
SET @rowCount = RowCount(@test)
ENDIF
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