Kingdom Hearts as a narrative is best understood as two children coming up with increasingly convoluted reasons why they won the imaginary play fight they're having.
Sora Kid: I beat you!
Xehanort Kid: Nuh-uh! Because I transfer my soul into one of your guys, so now I'm a cool younger version of myself with my own evil ghost friend.
Sora Kid: Oh yea? Well I transfer my soul into a random kid who looks like a younger version of me!
Xehanort Kid: Hey, no fair! You're copying me!
Sora Kid: Nuh-uh!
(And then it goes on like this for a billion games over twenty years.)