<LEVEL>
<PARENT>
<CHILD>A</CHILD>
</PARENT>
<SECTOR>1</SECTOR>
<PARENT>
<CHILD>B</CHILD>
</PARENT>
<SECTOR>2</SECTOR>
<PARENT>
<CHILD>C</CHILD>
</PARENT>
<SECTOR>3</SECTOR>
</LEVEL>
Hi, I am doing a for each loop for every 'PARENT' but also want to print SECTOR for each loop. Itried doing ../SECTOR but its repaeating the same SECTOR.
Required output:
CHILD SECTOR
1st Loop: A 1 2nd Loop: B 2 3rd Loop: C 3
Current output I am getting:
CHILD SECTOR
1st Loop: A 1 2nd Loop: B 1 3rd Loop: C 1
Can you please help/suggest?
Seems like you are depending on the order of which the PARENT elements and SECTOR elements occur within the XML, which is not ideal. If the PARENT elements are related tot he SECTOR elements, one of them should be within the other.
With that said, you can select the iteration of an element like this:
<?SECTOR[1]?>
Where 1 is the first iteration. So if you used [2] it would select the value of the second iteration. But you want this to be dynamic, so we cant hard code the iteration number.
Combined with a variable that will count the iteration you are on, here is the solution:
<?for-each:LEVEL?>
<?xdoxslt:set_variable($_XDOCTX,'COUNTER', 1)?>
<?for-each:PARENT?>
Child: <?CHILD?>
Sector:<?../SECTOR[xdoxslt:get_variable($_XDOCTX, 'COUNTER')]?>
<?xdoxslt:set_variable($_XDOCTX, 'COUNTER', xdoxslt:get_variable($_XDOCTX, 'COUNTER') + 1)?>
<?end for each?>
<?end for each?>
I HIGHLY recommend changing the XML to solve your problem rather than relying on the elements to be in the correct order.