Welcome to TRS
Explorer Ranger body swap won't work too well, different wheel bases and frames, it can be done, just needs lots of modifications.
And what are the other vehicle specs, 2WD or 4WD on explorer, 2.3l would be 2WD most likely, are both automatics?
2.3l transmission won't bolt to the 4.0l
And if 2.3l is a manual then you need to ditch the brake clutch pedal assembly and put in a different steering column for the explorer automatic, but you could setup a universal floor shifter for the automatic.
4.0l SOHC will need new timing chains while it is out.
But the 4.0l and it's trans can be put in the Ranger, along with it's computer and wiring harness.
If Explorer has an automatic then it's computer will only work well with that automatic model.
If it is 4WD/AWD then you can just bring over the transfer case with trans and not use it for 4WD, or you would have to get that model transmission in a 2WD version for a 4.0l, they can't be converted without total rebuild.
Then you have the fuel system to deal with, 1997 explorer used a Return line fuel system running 35-40psi pressure
1998 Ranger used a Returnless system using 65-70psi pressure.
1997 computer will be expecting 35-40psi, Read post #4 here:
http://www.therangerstation.com/forums/showthread.php?t=157576
It is related to fuel pressure so 5.0l or 4.0l doesn't matter, same modification is needed
You will need a new drive shaft, and the 2.3l will have 7.5" rear axle, so swapping over the explorer 8.8" axle would be best, if its OK
There are more differences but these just come to mind.