Fix the out-of-bound failure in stylesheet iterator.

Rewrite the iterator as internal instead of external.
This is much easier for recursive tree traversal.