2

I have a field that is receiving a list of values formatted in XML. I have a loop, however, the loop is only displaying one set of products. Below is the code I am using.

 <?xml version="1.0" encoding="utf-16"?>

<ArrayOfProducts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <Products>

    <Name>PHONE PROTECTION PLAN</Name>

    <ImageUrl>All_Phone_Protection_Plan-small.png</ImageUrl>

    <Price>5.99</Price>

    <Quantity>1</Quantity>

  </Products>

</ArrayOfProducts><?xml version="1.0" encoding="utf-16"?>

<ArrayOfProducts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <Products>

    <Name>TABLET PROTECTION PLAN</Name>

    <ImageUrl>All_Tablet_Small.png</ImageUrl>

    <Price>5.99</Price>

    <Quantity>1</Quantity>

  </Products>

</ArrayOfProducts>

This is the AMPscript.


%%[                                           
Set @ProductInfoXML = Product_Info
Set @rowsetArrayOfProducts = BuildRowsetFromXML(@ProductInfoXML, '//ArrayOfProducts', 1)
Set @rowsetProductName = BuildRowsetFromXML(@ProductInfoXML, '//Name', 1)
Set @rowsetProductImage = BuildRowsetFromXML(@ProductInfoXML, '//ImageUrl', 1)
Set @rowsetProductQuantity = BuildRowsetFromXML(@ProductInfoXML, '//Quantity', 1)
Set @rowsetProductPrice = BuildRowsetFromXML(@ProductInfoXML, '//Price', 1)


for @i = 1 to Rowcount(@rowsetArrayOfProducts) do


if (Rowcount(@rowsetProductName) >= @i) then
    Set @Product_Name = Field(Row(@rowsetProductName,@i),1) 
  else 
    set @Product_Name = ''
  endif

if (Rowcount(@rowsetProductImage) >= @i) then
    Set @Product_Image = Field(Row(@rowsetProductImage,@i),1) 
  else 
    set @Product_Image = ''
  endif

if (Rowcount(@rowsetProductQuantity) >= @i) then
    Set @Product_Quantity = Field(Row(@rowsetProductQuantity,@i),1) 
  else 
    set @Product_Quantity = ''
  endif

  if (Rowcount(@rowsetProductPrice) >= @i) then
    Set @Product_Price = Field(Row(@rowsetProductPrice,@i),1) 
  else 
    set @Product_Price = @rowsetProductPrice
  endif

  ]%%



                                                
Product Name: %%=ProperCase(@Product_Name)=%%
Price: $%%=v(@Product_Price)=%%
Quantity: %%=v(@Product_Quantity)=%% %%[next @i]%%

Thanks, Gaudy

1 Answer 1

3

Prolog present two times and root node were missing in XML file. I used small AmpScript to overcome this issue.

    %%[
Set @ProductInfoXML = Product_Info
SET @ProductInfoXML = Replace(@ProductInfoXML,'<?xml version="1.0" encoding="utf-16"?>','')
SET @ProductInfoXML = ConCat('<?xml version="1.0" encoding="utf-16"?><root>',@ProductInfoXML,'</root>')
Set @rowsetArrayOfProducts = BuildRowsetFromXML(@ProductInfoXML, '//ArrayOfProducts', 1)
Set @rowsetProductName = BuildRowsetFromXML(@ProductInfoXML, '//Name', 1)
Set @rowsetProductImage = BuildRowsetFromXML(@ProductInfoXML, '//ImageUrl', 1)
Set @rowsetProductQuantity = BuildRowsetFromXML(@ProductInfoXML, '//Quantity', 1)
Set @rowsetProductPrice = BuildRowsetFromXML(@ProductInfoXML, '//Price', 1)


for @i = 1 to Rowcount(@rowsetArrayOfProducts) do


if (Rowcount(@rowsetProductName) >= @i) then
    Set @Product_Name = Field(Row(@rowsetProductName,@i),1) 
  else 
    set @Product_Name = ''
  endif

if (Rowcount(@rowsetProductImage) >= @i) then
    Set @Product_Image = Field(Row(@rowsetProductImage,@i),1) 
  else 
    set @Product_Image = ''
  endif

if (Rowcount(@rowsetProductQuantity) >= @i) then
    Set @Product_Quantity = Field(Row(@rowsetProductQuantity,@i),1) 
  else 
    set @Product_Quantity = ''
  endif

  if (Rowcount(@rowsetProductPrice) >= @i) then
    Set @Product_Price = Field(Row(@rowsetProductPrice,@i),1) 
  else 
    set @Product_Price = @rowsetProductPrice
  endif

  ]%%                                              
Product Name: %%=ProperCase(@Product_Name)=%% <br/>                                    
Price: $%%=v(@Product_Price)=%% <br/>                                                 
Quantity: %%=v(@Product_Quantity)=%% <br/>   

%%[Next @i]%%

Output:

Product Name: Phone Protection Plan
Price: $5.99 
Quantity: 1 
Product Name: Tablet Protection Plan 
Price: $5.99 
Quantity: 1
0

You must log in to answer this question.

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