As soon as a nested span comes I want to close the outer richtext and start a new richtext for inner and then open a richtext after inner one closes.
As I said, requirements like this are extremely difficult to implement in XSLT. You will not get anywhere until you rephrase that in terms of elements and nodes.
Let's try this as requirements: 1. If a text node is a descendant of one or more span elements, it should be replaced in the output tree by a richtext element containing only that text node. 2. A span element should be replaced in the output tree by its text descendants with requirement 1 applied.
This translates into XSLT as
Let's see if that works for a start.