3

I'm having trouble referencing a specific item in our order XML feed. I want to reference a specific "attribute-id", not the entire rowset.

XML reference:

<custom-attributes>
        <custom-attribute attribute-id="recipientName">JeremyTest</custom-attribute>
        <custom-attribute attribute-id="senderName">Jeremy</custom-attribute>
</custom-attributes>

My current code:

Set @giftMessageToFrom = BuildRowsetFromXML(@xml,'//shipment/custom-attributes/custom-attribute',1)

I would like to be able to check if it is a "recipientName" or "senderName" as sometimes customers will only put one or the other, not both.

Please also link to any documentation on this. I can't find anything specific to id's like this in XMLs. I've tried going the "Field" route to no avail along with trying to put the entire thing into the BuildRowsetFromXML but that also didn't work.

Thanks!

4

The online documentation for the BuildRowSetFromXML function isn't very clear on how to reference attribute values, but if you pay attention to the table at the bottom of the page, you'll notice that the column headers mirror the attribute names, but with the string "_att" appended to each (i.e., attribute-id_att is what you'll want to use).

With that, using your XML snippet above, the following code:

%%[
var @anonymousNode,
    @attrId,
    @attrSuffix,
    @custAttr,
    @i,
    @row,
    @rowCount,
    @rowset,
    @xmlPartial

set @xmlPartial = 
'<custom-attributes>
   <custom-attribute attribute-id="recipientName">JeremyTest</custom-attribute>
   <custom-attribute attribute-id="senderName">Jeremy</custom-attribute>
</custom-attributes>'

set @anonymousNode = Concat('<root>', @xmlPartial, '</root>')
set @rowset = BuildRowsetFromXml(@anonymousNode, '//custom-attributes/custom-attribute', 1)
set @rowCount = RowCount(@rowset)

if (@rowCount > 0) then
  set @attrSuffix = '_att'

  for @i = 1 to @rowCount do
    set @row = Row(@rowset, @i)
    set @custAttr = Field(@row, 'Value')
    set @attrId = Field(@row, Concat('attribute-id', @attrSuffix))

    if (@attrId == 'recipientName') then
      Output(Concat(@custAttr, ' selected "recipientName"<br>'))
    elseif (@attrId == 'senderName') then
      Output(Concat(@custAttr, ' selected "senderName"<br>'))
    else
      Output(Concat(@custAttr, ' did not make a selection<br>'))
    endif

  next @i

else
  Output(Concat('Content unavailable'))
endif
]%%

Produces the following output:

JeremyTest selected "recipientName"
Jeremy selected "senderName"
  • 1
    This worked great. Thanks! – Jeremy Altman Mar 18 '19 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.