Skip to content

Instantly share code, notes, and snippets.

@wvpv
Last active March 3, 2020 17:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wvpv/cc72b3662c43ba2c1650cccc979ee137 to your computer and use it in GitHub Desktop.
Save wvpv/cc72b3662c43ba2c1650cccc979ee137 to your computer and use it in GitHub Desktop.
Parse ASC XML Payload
<html>
<head>
<style>
body, a, input {font-family:sans-serif;}
</style>
</head>
<body style="font-family:sans-serif">
%%=now()=%%
<h1>XML Parse with AMPScript</h1>
%%[
/* 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')
endif
if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"name"))) > 0 then
set @name = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"name"),0),1),'Value')
endif
if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"url"))) > 0 then
set @url = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"url"),0),1),'Value')
endif
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>
%%[
endif
next @i
else
outputline(concat("<br>no products found"))
endif
else
outputline(concat("<br>no XML found"))
endif
]%%
<br><br><br><a href="%%profile_center_url%%">Profile/Preference Center</a>
<br><a href="%%subscription_center_url%%">Subscription Center</a>
<br><a href="%%unsub_center_url%%">One-Click Unsubscribe</a>
<p>This email was sent by:
<b>%%Member_Busname%%</b>
<br>%%Member_Addr%%, %%Member_City%%, %%Member_State%% %%Member_PostalCode%% %%Member_Country%%</p>
<custom name="opencounter" type="tracking">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment