0

For example if there are nested entities in an XML string I would need to loop over the parent entities and the child entities (like a repeater within a repeater in ASP.NET). If you would like an example of such XML I can paste it here.

As requested, an example snippet of what I am trying to achieve:

%%[ Var @xml, @names, @talents, @talent, @rows

Set @xml = '
<root>
    <parent-object>
        <name>Dan</name>
        <surname>Surname</surname>
        <talents>//child object to obtain which would be acquired through nested for loop
            <talent-name>fast worker</talent-name>
            <experience>2 years</experience>
            <talent-name>typing</talent-name>
            <experience>3 years</experience>
        </talents>

    </parent-object>

    <parent-object>
        <name>John</name>
        <surname>Surname</surname>
        <talents> //child object to obtain which would be acquired through nested for loop
            <talent-name>runqning</talent-name>
            <experience>2 years</experience>
            <talent-name>walking</talent-name>
            <experience>3 years</experience>
        </talents>
    </parent-object>

</root>'




Set @names = BuildRowsetFromXML(@xml,"//root/parent-object/name",1)

Set @talents = BuildRowsetFromXML(@xml,"//root/parent-object/talents/talent-name",1)



SET @rows = RowCount(@names)
for @cnt = 1 to @rows do //start loop

Set @name = Field(Row(@names,@cnt), "Value")
Set @talent = Field(Row(@talents,@cnt), "Value")



]%%


 <div>  
  - %%=v(@name)=%% <br>


 Talents: %%=v(@talent)=%% <!-- //displays single talent. Here is where i would require an inner for loop to iterate over talents. I would like to do this without separating the xml -->
 </div>





%%[next @cnt]%%
6
  • 1
    Please update your question with some of your work-in-progress code along with an XML sample. Sep 21, 2015 at 20:01
  • @AdamSpriggs, please see updated question
    – Sean McC
    Sep 24, 2015 at 13:35
  • I think my answer to this other question is appropriate here. Sep 24, 2015 at 13:39
  • As for the nested talents node, you can always build another rowset inside your for loop. Sep 24, 2015 at 13:42
  • @AdamSpriggs, How would i build the rowset so that it relates to the current iteration of the for loop?
    – Sean McC
    Sep 25, 2015 at 14:41

1 Answer 1

2

For iterating through the child elements, you'll need to utilize the XPATH index to limit the scope of the BuildRowSetFromXML() results (as in the example I cited in my comment). See the Set @talents = line below:

%%[

Var @xml, @i, @j, @names, @talents, @talent, @rowCount1, @rowCount2

Set @xml = "
<root>
    <parent-object>
        <name>Dan</name>
        <surname>Surname</surname>
        <talents>
            <talent-name>fast worker</talent-name>
            <experience>2 years</experience>
            <talent-name>typing</talent-name>
            <experience>3 years</experience>
        </talents>
    </parent-object>
    <parent-object>
        <name>John</name>
        <surname>Surname</surname>
        <talents>
            <talent-name>runqning</talent-name>
            <experience>2 years</experience>
            <talent-name>walking</talent-name>
            <experience>3 years</experience>
        </talents>
    </parent-object>
</root>"

set @names = BuildRowsetFromXML(@xml,"//root/parent-object/name",1)
set @rowCount1 = RowCount(@names)
outputline(concat("<br>rowCount1: ",@rowCount1))
for @i = 1 to @rowCount1 do

    Set @name = Field(Row(@names,@i), "Value")
    outputline(concat("<br><br>name: ",@name))

    Set @talents = BuildRowsetFromXML(@xml,concat("//root/parent-object[",@i,"]/talents/talent-name"),1)

    set @rowCount2 = rowCount(@talents)
    outputline(concat("<br>rowCount2: ",@rowCount2))

    for @j = 1 to @rowCount2 do
         Set @talent = Field(Row(@talents,@j), "Value")
         outputline(concat("<br>&nbsp;talent: ",@talent))
    next @j

next @i
]%%

Output

rowCount1: 2 

name: Dan 
rowCount2: 2 
 talent: fast worker 
 talent: typing 

name: John 
rowCount2: 2 
 talent: runqning 
 talent: walking 

You must log in to answer this question.

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