
Perhaps the trickiest thing about XSLT is understanding the order in which things happen, and the logic that determines what comes next. It is important to understand this because, otherwise, you may get some puzzling results.
The processor begins with the root of the input tree (for now, we’ll pretend that’s also the root element of the document). Then the processor goes to the stylesheet to look for the template that matches that root. When it finds the matching template, it does what the stylesheet says.
If the template contains an instruction to continue applying templates, the processor goes back to the input tree to consider the children of the matched element. For each child, it goes back to the stylesheet to see if there are templates to match it. And on and on until it reaches the end of the stylesheet.
We might be used to thinking of computer programs as sets of steps that will
happen in order, where the sequence of events is determined by the computer
program itself. XSLT is different in that the ordering of the steps is
determined by the input tree. As you can see here in the example, since
<opener> appears in the input document before <l>, the
<opener> template is applied first, even though it appears after
the <l> template in the stylesheet. Thus, the <h2> appears
before the two <p> elements in our output document.