When saxon:stream (or xsl:copy-of with read-once) is used to perform streaming, some other optimizations are suppressed to ensure that the code remains streamable. Unfortunately this also suppresses certain operations on the subtree below the StreamingCopy expression that are necessary to keep the tree consistent, for example when references to one variable are replaced by references to another. This can produce internal errors such as
java.lang.IllegalStateException: *** Internal Saxon error: local variable encountered whose binding has been deleted
A patch is being raised on the 9.3 branch with two effects: the StreamingCopy instruction becomes better-behaved at respecting optimizations unrelated to streaming, and for extra robustness, the optimization whereby references to one variable are replaced by references to another now performs a check on completion that all references have indeed been replaced, and abandons the optimization if not.