Same as here, use the id of the first preceding sibling whose name is different in order to group the records:
But the output i am getting is not same as my desired output...
When applied to your (cleaned up) input of:
bar
bar
foobar
foobar
foobar
bar
bar
the result is:
bar
bar
foobar
foobar
foobar
bar
bar
which to me seems to be exactly the result you have asked for:
bar
bar
foobar>
foobar>
foobar>
bar
bar
except for the >
characters inside the
elements, which I have removed from the input.
--