I'm trying to create an email that pulls XML from a field in a data extension but my ampscript knowledge is very limited.

The DE field that the XML is under is a text field called 'attribute' and here is the XML file:


I know that I need to use the BuildRowsetFromXml function but I'm getting varying suggestions from different sources and would love some advice!

1 Answer 1


I'd recommend looking over how XPATH 1.0 works -- that's the key part to understanding how to pull node attributes and values from the payload.

Here's an example with a different, but similar payload.


/* Sample XML Payload: https://gist.github.com/wvpv/8bec8dc8774fdd1fedb25a69071ac905 */

var @xml, @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"))




  • A good answer. Depending on whether you're doing much manipulation of the XML data or just wanting to present the data in your email, you could also consider TransformXML as an alternative - developer.salesforce.com/docs/…. It can prove to be a little more maintainable than using AMPScript to manipulate and present XML based data.
    – Macca
    Commented May 7, 2020 at 2:38

You must log in to answer this question.

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