0

XML field DISCAMT is added multiple times using varying values and I'm interested to total these and show in one line. What function can be used here or just create new field/s per each product purchased (DISCAMT2, DISCAMT3) in the API and use an add function.

EX:

DISCAMT="3.50"
DISCAMT="4.00"
1
  • Can you provide a sample of the XML that you are referencing? Commented Mar 3, 2020 at 18:48

1 Answer 1

2

If your XML was something like this:

<?xml version="1.0" encoding="UTF-8"?>
<cart>
      <items>
            <item>
                  <sku><![CDATA[123]]></sku>
                  <discAmt>0.1</discAmt>
            </item>
            <item>
                  <sku><![CDATA[456]]></sku>
                  <discAmt>0.2</discAmt>
            </item>
            <item>
                  <sku><![CDATA[789]]></sku>
                  <discAmt>0.3</discAmt>
            </item>
      </items>
</cart>

Then you could loop through the nodes in your XML and use the add function to keep a running total of the dsctAmt values.

%%[

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

    var @totalDiscAmt 
    set @totalDiscAmt = 0

    for @i = 1 to @rowCount do

      var @nodepath
      var @sku
      var @discAmt

      set @discAmt = 0

      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,"discAmt"))) > 0 then
          set @discAmt = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"discAmt"),0),1),'Value')
          set @totalDiscAmt = add(@totalDiscAmt,@discAmt)
      endif

      if not empty(@sku) then

      ]%%

       <br>sku: %%=v(@sku)=%% %%=v(@discAmt)=%%</a>

      %%[

      endif

    next @i

    ]%%

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

    %%[

  else

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

  endif

else

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

endif

]%%
2
  • XML feeds into field "XML_Data" off the DE <?xml version="1.0" encoding="utf-8"?><Email><ClientInformation DISCNAME="10_percent_discount_" DISCQTY="1" DISCAMT="3.5"DISCNAME="10_percent_discount_" DISCQTY="1" DISCAMT="4" Are you proposing to wrap with if indexOf(@xml,"<Email>") > 0 then etc..
    – Matt S.
    Commented Mar 3, 2020 at 18:01
  • I'm not suggesting any particular XML structure, just providing an example of doing the math. SFMC is fine with valid XML, but you're limited to XPATH 1.0 specifications in the BuildRowSetFromXML function. Commented Mar 3, 2020 at 18:06

You must log in to answer this question.

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