1

I'm trying build an newsletter that populates the five most recent stories from our website.

I'm using this AMPscript to parse the XML:

    %%[Var @xml, @xml1, @titles, @authors, @author, @descs, @desc, @links, @link, @images, @image, @cnt
Set @xml1 = ContentAreaByName("My Contents\My feed") /*This line specifies the content area from which the RSS content will be pulled for the email message.*/

Set @xml = Replace(@xml1,'media:content','mediacontent')
Set @titles = BuildRowsetFromXML(@xml,"//item/title",1)
Set @descs = BuildRowsetFromXML(@xml,"//item/description",1)
Set @links = BuildRowsetFromXML(@xml,"//item/link",1)
Set @authors = BuildRowsetFromXML(@xml,"//item/author",1)
Set @images = BuildRowsetFromXML(@xml,"//item/mediacontent",1)
If RowCount(@titles) > 5 THEN
SET @rows = 5
ELSE
SET @rows = RowCount(@titles)
ENDIF
IF @rows >= 1 THEN
for @cnt = 1 to 5 do
Set @title = Field(Row(@titles,@cnt),"Value")
Set @desc = Field(Row(@descs,@cnt), "Value")
Set @link = Field(Row(@links,@cnt), "Value")
Set @author = Field(Row(@authors,@cnt), "Value")
Set @image = Field(Row(@images,@cnt), "url_att")]%%

The problem is that not every item in the RSS has a media:content element. So ExactTarget is spitting out an error. How can I modify this AMPscript to work even when one of the 5 most recent items lacks a media:content element?

Thanks!

I've tried to figure out how to put the answer provided into practice, but I don't think I understand it fully. This is what I have right now:

Set @xml = Replace(@xml1,'media:content','mediacontent')
Set @titles = BuildRowsetFromXML(@xml,"//item/title",0)
Set @descs = BuildRowsetFromXML(@xml,"//item/description",0)
Set @links = BuildRowsetFromXML(@xml,"//item/link",0)
Set @authors = BuildRowsetFromXML(@xml,"//item/author",0)
Set @images = BuildRowsetFromXML(@xml,"//item/mediacontent",0)

Set @rows = RowCount(@titles)

for @i to @rows

if RowCount(BuildRowsetFromXML(@xml,concat("//item[",@i,"]/title"),0)) > 0 then
Set @title = Field(Row(BuildRowsetFromXML(@xml,concat("//item[",@i,"]/title"),0),1),"Value")
endif

if RowCount(BuildRowsetFromXML(@xml,concat("//item[",@i,"]/description"),0)) > 0 then
Set @description = Field(Row(BuildRowsetFromXML(@xml,concat("//item[",@i,"]/description"),0),1),"Value")
endif

if RowCount(BuildRowsetFromXML(@xml,concat("//item[",@i,"]/link"),0)) > 0 then
Set @link = Field(Row(BuildRowsetFromXML(@xml,concat("//item[",@i,"]/link"),0),1),"Value")
endif

if RowCount(BuildRowsetFromXML(@xml,concat("//item[",@i,"]/author"),0)) > 0 then
Set @author = Field(Row(BuildRowsetFromXML(@xml,concat("//item[",@i,"]/author"),0),1),"Value")
endif

if RowCount(BuildRowsetFromXML(@xml,concat("//item[",@i,"]/mediacontent"),0)) > 0 then
Set @image = Field(Row(BuildRowsetFromXML(@xml,concat("//item[",@i,"]/mediacontent"),0),1),"url_att")
endif

But I don't think this is correct. Any help with what I'm misunderstanding would be greatly appreciated. Thank you.

  • Have you considered using an XSLT for this? It could be far more forgiving than hard coding AMPScript in this way., applying templates as an when they're matched. – Macca Apr 10 '16 at 8:43
  • @Macca, thanks for the suggestion. I'm new to Exact Target/AMPscript and have to admit I'm unfamiliar with XSLT. I'll look into though! – Jeff Ayers Apr 12 '16 at 15:28
0

My suggestion would be to check the rowcount() first. I generally structure it like this, so you don't assume that all of the nodes have the same tags. You can nest this same structure as dictated by the structure of your XML.

%%[

 set @Nodes = BuildRowsetFromXML(@xml,"/collection/items/*",0)
 set @rowCount = rowcount(@Nodes)

 for @i = 1 to @rowCount do

     if rowcount(BuildRowsetFromXML(@xml,concat("/collection/items/item[",@i,"]/var1"),0)) > 0 then

      set @var1 = Field(Row(BuildRowsetFromXML(@xml,concat("/collection/items/item[",@i,"]/var1"),0),1),'Value')

     endif

     ]%%

     <br>@var1: %%=v(@var1)=%%

%%[ next @i ]%%
  • Adam, thank you for the help. I'm new to AMPscript and Exact Target, so please forgive any beginner questions. What is the significance of @i in your example? Does it signify an item in the XML file? And I'm unclear what you mean by nesting the structure "as dictated by the structure of your XML." – Jeff Ayers Apr 12 '16 at 15:41
  • It's just an iterator/index variable. It varies with every row in the result-set. – Adam Spriggs Apr 12 '16 at 15:42
  • I'm still having trouble. Does the snippet you've suggested work with the current AMPscript I'm using? Or does it replace it? What I'm using currently is just a slightly modified version of the RSS script Exact Target provides in their help section. – Jeff Ayers Apr 12 '16 at 18:28
  • You'll need to update my code with the nodes from your RSS feed. – Adam Spriggs Apr 12 '16 at 20:20

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.