Welcome to TRS
I believe an engine swap needs the newer motor to BE a newer motor so in your case 2006 or newer motor.
And while you could do a 4.0l swap into 3.0l Ranger it would not be a great swap to do since you can just go out and buy a 4.0l stock Ranger, which as said above, you would need to do anyway.
If your 3.0l engine is bad then either replace it/fix it and sell it to buy a 4.0l Ranger, or sell it "as is" mechanics special and buy a 4.0l Ranger
To swap in a 4.0l into a 3.0l Ranger you need the 4.0l engine and 2WD TRANSMISSION, 3.0l trans won't work with 4.0l
You also need the 4.0l's computer and complete engine wiring harness, and trans harness if automatic.
So basically the same work and cost as V8 swap but no real valve added since you could go out and buy a stock 4.0l 2WD Ranger for probably less cost in time and money and you would know it would pass inspection.
Even if you follow all the rules for engine swapping in Texas, there is no guarantee it will pass inspection the 1st time