Supplied node must be
built using the same or a compatible Configuration

shows that you have more than one Configuration object around,
so perhaps you are setting the attribute on the wrong one.

If you use multiple JAXP factories and share nodes between
them, then you must make sure they all use the same underlying Saxon
Configuration. (Frankly, if you're trying to do this kind of thing, it's much
cleaner to forget JAXP interfaces and use s9api instead).

As an alternative to setting optimization level to zero, try
using a variable that won't be inlined, for example because there are multiple
references to it. And try doing the equivalent of -explain to view the optimized
code before you trace it (from the API, you can use the ExpressionPresenter to
display a compiled expression).