0

Within an RSS feed I am trying to target the img>src from the description tag

    <description>&lt;img src="https://www.website.com/content/dam/cmegroup/videos/metals-market-commentary-bob-iaccino-3-4-22.jpg"/&gt;
Flight-to-safety trade pushes gold to six-month highs. Bob Iaccino discusses.

#metals
#gold

Learn More: 

https://www.website.com/markets/metals.html
&lt;!-- 
&lt;meta property="og:title" content="Metals Market Commentary: Bob Iaccino, 3/4/22" /&gt;
&lt;meta property="og:type" content="website"&gt;
&lt;meta property="og:url" content="https://www.website.com/videos/2022/03/04/metals-market-commentary-bob-iaccino-3-4-52.html" /&gt;
&lt;meta property="og:image" content="https://www.website.com/content/dam/cmegroup/videos/metals-market-commentary-bob-iaccino-3-4-22.jpg" /&gt; --&gt;
</description>

I can get the description using

Set @descs = BuildRowsetFromXML(@xml,"//item/description,1)

but I can't drill down to the src attribute in the first line.

Any help would be really appreciated.

Thanks

3
  • 1
    Doesn't look like you're wanting to parse RSS -- the value for the description node should be a treated as a string, not XML. Mar 7, 2022 at 14:30
  • Hi @AdamSpriggs Do you know how I could convert that to a string and pull out the value of src attribute?
    – GerardD
    Mar 7, 2022 at 15:06
  • Please delete your duplicate question on StackOverflow. There are way more eyes on SFMC and AMPscript questions here than there. Mar 8, 2022 at 13:27

1 Answer 1

0

Getting the embedded description from a string value is an appropriate use of the regexmatch AMPscript function, IMO.

I'd recommend using regex101.com to test your patterns. Here's my test for your example.

%%[

var @xml, @isXML, @nodes, @rowCount
set @xml = HTTPGet("http://feeds.feedburner.com/allfeaturedreports", false, 0, @CallStatus)

/* strip out invalid elements if needed */
/* set @xml = Replace(@xml, "dscblog:","") */

output(concat("<br>Call Status: ", @CallStatus))

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

  set @nodes = BuildRowsetFromXML(@xml,"/rss/channel/item",0)
  set @rowCount = rowcount(@nodes)

  output(concat("<br>rowCount: ", @rowCount))

  if @rowCount > 0 then

    for @i = 1 to @rowCount do

      var @pubDate
      var @title
      var @description
      var @link

      set @nodepath = concat("/rss/channel/item[",@i,"]/")

      /* output(concat("<br>nodepath: ", @nodepath)) */

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

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

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

          set @pattern = 'img\ssrc=\"(.*?)\"'
          set @imgSrc = RegExMatch(@description, @pattern, 1)
          output(concat("imgSrc: ",@imgSrc,"<br>"))

      endif

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

      ]%%

         <h4>%%=v(@title)=%%</h4>
         %%=v(@pubDate)=%%
         <p>
           %%=v(@description)=%%
           <br><a href="%%=redirectto(@link)=%%">%%=v(@link)=%%</a>
         </p>


      %%[

    next @i

  else

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

  endif

else

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

endif

]%%

Code execution: https://mcsnippets.herokuapp.com/s/LR11N8Br

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .