If the problem comes and go erratically, and can come back when you hit a bump, that is probably a contact.
A bad contact to one of the sensors (eg.O2 sensor, or TPS if it is only when in gear) can cause the engine running rough.
The way to find out is scanning the ECU (main control module).
this cannot be done with a standard scanner because this car is not OBD2 compliant.
I heard that
Memoscan U581 can be used on older XJ series models, this info must be verified asking manufacturer.
The alternative is using an
Elm327 adapter to connect to a laptop with
AutoEnginuity software, or aving the computer module scanned at an authorized garage.
Once the scanner returns the codes, their meaning can be checked online posting here the code.
Regards.