If both are the diesel, then yes, it should work. 4X4 for 4X4 trucks, 4X2 for 2WD trucks. Also keep in mind that if it's an auto, things get dicey as it's computer controlled, and the computers between both cars may be different (as well as trans model, though I'm not sure).
Most wrecking yards have this info and can tell what years parts are interchangeable and what aren't. Unless you know the wrecking yard counter guy, getting them to look that up for you may be difficult.
Also, even though the replacement trans may bolt right up the electrical\computer controlled part may be different. Take a good look at the electrical connector and make sure they're the same.