That is a big question! I'm sure there are lots of folks who think about it lots of different ways. Pulling an engine from a yard can be an undertaking. The 4.0 is pretty heavy!
First, are you sure you need an engine? Can you just get a head? Less risky (and cheaper and easier) if you know your bottom end is good.
Obviously, it is best if you can grab an engine that you know works. Otherwise, look for something with low miles (well, for an XJ!) that has been wrecked. If it was wrecked, that is probably why it is in the scrap yard. If it isn't wrecked, it is more likely to have mechanical problems.
I assume that your scrap yard won't have fluids in the engine. Otherwise, you'd look at them. If you have fluids and a battery, you can always check compression. But then you should be able to start it up too!
If it is dry, I'd pop the valve cover and oil pan. Look for no sludge up top, no metal in the oil pan. I suppose if you were picky you could also take some plastigauge and measure the clearance on a bearing or two.
My biggest recommendation is to do the freeze plugs on any engine you have out. They are cheap and easy. When the rear one goes, you need to pull either the engine or tranny to get to it. Get good brass ones.
Oil pan and valve cover gaskets, for sure. I'd change the rear main seal and put in a new oil pump, but only a good OEM quality piece (like a Melling--sticking with a high mileage stock pump might be a better bet than getting a cheapie). I bet your exhaust manifold is cracked. I'm having really good luck with the ATP stainless, for the money. I like the Spectra radiators for a mere $70. If you've got the original radiator, you are taking it out anyway.
Finally, take your time and read a lot. There are also many potential pit falls. For example, if you goof up torquing the rear main cap, you could destroy your new engine. If you aren't careful with a 500lb engine high up on a hoist, you could hurt yourself or a friend really badly. But, read up, have fun, and you'll learn a lot!
Paul