This happened on mine, took forever to find good info, but the problem was the AC Clutch "air gap". What happens is the ac clutch wears over time from it engaging/ disengaging every few seconds, and the gap between the clutch and the pulley gets bigger and bigger over time. Since these things are operated via electro-magnets, when the gap gets bigger, it makes it harder for the magnet to pull in the clutch to engage it. The reason it will work for a little while and then stop is because when underhood temps rise, the resistance in the clutch coil rises and thus the magnetec force gets weaker. To fix this all you need to to is take off the serpentine belt, unbolt the 4 bolts on top of the compressor that hold it down (leave the lines connected) and pull the compressor up so you can access the main bolt in the center of the clutch. Loosen the bolt (I used an impact, though not neccissary, its only tightened to like 22 lb/pounds), then use two screw drivers to gently pry the front clutch face off. when that is off you will see 2 little washers on the shaft, take one of those washers out to bring the clutch closer to the pulley, assembly is the reverse of removal, and voala!! Hopefully this fixes your problem. BTW mine is a 98, and I just bought it, got a good deal on it cause the AC didnt work right
.
Here is a link the the manual covering this procedure, our 97+ XJ's have the sanden SD7H15 compressor.
http://www.sanden.com/support/pdf/sd7servicemanual.pdf
Good luck
-Jerry