0

I am trying to display multiple results using this loop. However, it will only loop through the first set of results from the XML.

AMPscript:

%%[

VAR @ShipmentsReleasedRows
SET @ShipmentsReleasedRows = BuildRowSetFromXML(@order_xml, "//root/ShipmentClassification", 1)
SET @ShipmentsReleasedRowsCount = RowCount(@ShipmentsReleasedRows)

/* Begin ShipmentsReleasedRowCount Loop */
IF @ShipmentsReleasedRowsCount > 0 THEN
FOR @a = 1 TO @ShipmentsReleasedRowsCount DO

    SET @ShipmentsReleasedRow = Row(@ShipmentsReleasedRows, @a)
    SET @ShipmentsReleasedRow_xml = Field(@ShipmentsReleasedRow ,"XML")
    SET @ShipmentsReleasedRow_xml = Concat('<root>',@ShipmentsReleasedRow_xml,'</root>')


        SET @CLASSIF_DISPLAY_ORDER = BuildRowSetFromXML(@ShipmentsReleasedRow_xml, "//root/CLASSIF_DISPLAY_ORDER", 1)
    IF RowCount(@CLASSIF_DISPLAY_ORDER) > 0 THEN
        SET @CLASSIF_DISPLAY_ORDER = Field(Row(@CLASSIF_DISPLAY_ORDER, 1), "Value")
    ELSE
        SET @CLASSIF_DISPLAY_ORDER = "0"
    ENDIF


    SET @STATUS_DISPLAY_ORDER = BuildRowSetFromXML(@ShipmentsReleasedRow_xml, "//root/ShipmentStatus/STATUS_DISPLAY_ORDER", 1)
    IF RowCount(@STATUS_DISPLAY_ORDER) > 0 THEN
        SET @STATUS_DISPLAY_ORDER = Field(Row(@STATUS_DISPLAY_ORDER, 1), "Value")
    ELSE
        SET @STATUS_DISPLAY_ORDER = "0"
    ENDIF


        SET @CLASSIF_NAME = BuildRowSetFromXML(@ShipmentsReleasedRow_xml, "//root/CLASSIF_NAME", 1)
    IF RowCount(@CLASSIF_NAME) > 0 THEN
        SET @CLASSIF_NAME = Field(Row(@CLASSIF_NAME, 1), "Value")
    ELSE
        SET @CLASSIF_NAME = "No Name"
    ENDIF


        SET @STATUS_NAME = BuildRowSetFromXML(@ShipmentsReleasedRow_xml, "//root/ShipmentStatus/STATUS_NAME", 1)
    IF RowCount(@STATUS_NAME) > 0 THEN
        SET @STATUS_NAME = Field(Row(@STATUS_NAME, 1), "Value")
    ELSE
        SET @STATUS_NAME = "No Name"
    ENDIF

]%%

XML:

<ShipmentClassification>
    <CLASSIF_CODE>HELD</CLASSIF_CODE>
    <CLASSIF_NAME>Held</CLASSIF_NAME>
    <CLASSIF_DISPLAY_ORDER>10</CLASSIF_DISPLAY_ORDER>
    <TOTAL_COUNT>0</TOTAL_COUNT>
    <ShipmentStatus>
        <STATUS_CODE>PENDING_RELEASE</STATUS_CODE>
        <STATUS_NAME>Pending Release</STATUS_NAME>
        <STATUS_DISPLAY_ORDER>20</STATUS_DISPLAY_ORDER>
        <TOTAL_COUNT>0</TOTAL_COUNT>
    </ShipmentStatus>
    <ShipmentStatus>
        <STATUS_CODE>PENDING_COMPLETION</STATUS_CODE>
        <STATUS_NAME>Pending Completion</STATUS_NAME>
        <STATUS_DISPLAY_ORDER>10</STATUS_DISPLAY_ORDER>
        <TOTAL_COUNT>0</TOTAL_COUNT>
    </ShipmentStatus>
</ShipmentClassification>

Current output:

Held 10
Pending Release 20

It is not displaying the "Pending Completion" row or the value in that row.

1 Answer 1

2

I'd suggest testing your XPATH in BuildRowSetFromXML using xpather. The path you've specified only returns one result:

xpather

You'll need to loop again once you get into the ShipmentClassification node. Something like this:

%%[

var @order_xml, @nodes, @rowCount
set @order_xml = AttributeValue("order_xml")

set @order_xml = ""
set @order_xml = concat(@order_xml,"<root>")
set @order_xml = concat(@order_xml,"    <ShipmentClassification>")
set @order_xml = concat(@order_xml,"        <CLASSIF_CODE>HELD</CLASSIF_CODE>")
set @order_xml = concat(@order_xml,"        <CLASSIF_NAME>Held</CLASSIF_NAME>")
set @order_xml = concat(@order_xml,"        <CLASSIF_DISPLAY_ORDER>10</CLASSIF_DISPLAY_ORDER>")
set @order_xml = concat(@order_xml,"        <TOTAL_COUNT>0</TOTAL_COUNT>")
set @order_xml = concat(@order_xml,"        <ShipmentStatus>")
set @order_xml = concat(@order_xml,"            <STATUS_CODE>PENDING_RELEASE</STATUS_CODE>")
set @order_xml = concat(@order_xml,"            <STATUS_NAME>Pending Release</STATUS_NAME>")
set @order_xml = concat(@order_xml,"            <STATUS_DISPLAY_ORDER>20</STATUS_DISPLAY_ORDER>")
set @order_xml = concat(@order_xml,"            <TOTAL_COUNT>0</TOTAL_COUNT>")
set @order_xml = concat(@order_xml,"        </ShipmentStatus>")
set @order_xml = concat(@order_xml,"        <ShipmentStatus>")
set @order_xml = concat(@order_xml,"            <STATUS_CODE>PENDING_COMPLETION</STATUS_CODE>")
set @order_xml = concat(@order_xml,"            <STATUS_NAME>Pending Completion</STATUS_NAME>")
set @order_xml = concat(@order_xml,"            <STATUS_DISPLAY_ORDER>10</STATUS_DISPLAY_ORDER>")
set @order_xml = concat(@order_xml,"            <TOTAL_COUNT>0</TOTAL_COUNT>")
set @order_xml = concat(@order_xml,"        </ShipmentStatus>")
set @order_xml = concat(@order_xml,"    </ShipmentClassification>")
set @order_xml = concat(@order_xml,"</root>")

if indexOf(@order_xml,"<root>") > 0 then

  set @nodes = BuildRowsetFromXML(@order_xml,"//root/ShipmentClassification",0)
  set @rowCount = rowcount(@nodes)

  if @rowCount > 0 then

    for @i = 1 to @rowCount do

      var @nodepath
      var @displayOrder
      var @classif_name
      var @url

      set @nodepath = concat("//root/ShipmentClassification[",@i,"]/")

      if rowcount(BuildRowsetFromXML(@order_xml,concat(@nodepath,"/CLASSIF_DISPLAY_ORDER"))) > 0 then
          set @displayOrder = Field(Row(BuildRowsetFromXML(@order_xml,concat(@nodepath,"/CLASSIF_DISPLAY_ORDER"),0),1),'Value')
      endif

      if rowcount(BuildRowsetFromXML(@order_xml,concat(@nodepath,"/CLASSIF_NAME"))) > 0 then
          set @classif_name = Field(Row(BuildRowsetFromXML(@order_xml,concat(@nodepath,"/CLASSIF_NAME"),0),1),'Value')
      endif

      output(concat("<br>nodepath: ", @nodepath))
      output(concat("<br>displayOrder: ", @displayOrder))
      output(concat("<br>classif_name: ", @classif_name))

      set @nodes2 = BuildRowsetFromXML(@order_xml, concat("//root/ShipmentClassification[",@i,"]/ShipmentStatus")) 
      set @rowCount2 = rowcount(@nodes2)

      if @rowCount2 > 0 then

        for @j = 1 to @rowCount2 do 

          var @nodepath2
          var @status_name
          var @status_display_order

          set @nodepath2 = concat("//root/ShipmentClassification[",@i,"]/ShipmentStatus[",@j,"]")

          if rowcount(BuildRowsetFromXML(@order_xml,concat(@nodepath2,"/STATUS_NAME"))) > 0 then
              set @status_name = Field(Row(BuildRowsetFromXML(@order_xml,concat(@nodepath2,"/STATUS_NAME"),0),1),'Value')
          endif

          if rowcount(BuildRowsetFromXML(@order_xml,concat(@nodepath2,"/STATUS_DISPLAY_ORDER"))) > 0 then
              set @status_display_order = Field(Row(BuildRowsetFromXML(@order_xml,concat(@nodepath2,"/STATUS_DISPLAY_ORDER"),0),1),'Value')
          endif

          output(concat("<br>nodepath2: ", @nodepath2))
          output(concat("<br>status_name: ", @status_name))
          output(concat("<br>status_display_order: ", @status_display_order))

        next @j

      endif

    next @i
  
  endif

endif

]%%

Output:

nodepath: //root/ShipmentClassification[1]/
displayOrder: 10
classif_name: Held
nodepath2: //root/ShipmentClassification[1]/ShipmentStatus[1]
status_name: Pending Release
status_display_order: 20
nodepath2: //root/ShipmentClassification[1]/ShipmentStatus[2]
status_name: Pending Completion
status_display_order: 10

You must log in to answer this question.

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