Define "match up." Will a transmission from a 2.9 bolt up to a 4.0? Yes.
Which 5 speed are you looking at (TK5, FM145, FM146, M5OD)? There were a few different 5 speeds used behind the 2.9, and many of them were different lengths. This means you may need to relocate the transmission cross member and/or shorten/lengthen the driveshafts.
Are the internals of said transmission strong enough to hold up to the 4.0? Depends on the transmission. The M5OD was also used behind the 4.0, so it should be. As for the others, Not sure. I've heard some say they can handle the 4.0, others say they can't.