Use Field(Row(BuildRowSetFromXML()) to fetch a field with a specific attribute-id from XML

ampscriptapimarketing-cloudtriggerxml

Issue
From the attached XML i am traying to read the value of "vertex_retail_delivery_fee" with below
code.

<custom-attribute attribute-id="vertex_retail_delivery_fee">0.27</custom-attribute>
<custom-attribute attribute-id="vertex_retail_delivery_fee_deducted_from_total_tax">5.07</custom-attribute>
<custom-attribute attribute-id="vertex_taxation_details">

Goal is to display the value if the xml has the attribute else it should be display "0.00" the below logic is throwing me error.

An error occurred when attempting to evaluate a RowCount function
call. See inner exception for details. xml parameter is invalid.
Function: BuildRowsetFromXML(@XML,
'custom-attributes/custom-attribute[@attribute-id="vertex_retail_delivery_fee"]')

Code:

if rowcount(BuildRowsetFromXML(@XML, '/custom-attributes/custom-attribute[@attribute-id="vertex_retail_delivery_fee"]')) > 0 
then
set @RetailFee= Field(Row(BuildRowsetFromXML(@XML, '/custom-attributes/custom-attribute[@attribute-id="vertex_retail_delivery_fee"]',0),1), 'Value'),'

Best Answer

Used this and it worked:

%%[
  SET @xml = ATTRIBUTEVALUE('xml')
  SET @rs = BuildRowSetFromXML(@xml, '/order/custom-attributes/custom-attribute', 0)
  SET @rc = ROWCOUNT(@rs)
  IF ROWCOUNT(@rs) > 0 THEN
    FOR @i = 1 to @rc DO
      SET @nodepath = CONCAT("/order/custom-attributes/custom-attribute[", @i, "]/")
      SET @attr = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath, "@attribute-id"),0),1),'Value')
      IF @attr == "vertex_retail_delivery_fee" THEN
        SET @value = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath, "text()"),0),1),'Value')
      ENDIF
    NEXT
  ENDIF
]%%
<hr>
value ==== %%=v(@value)=%%
<hr>
Related Topic