There are "door pins" at each door. These are basically just a plunger switch with two wires. It's a normally open circuit (with the door closed) ...as soon as the door opens, it closes the circuit, (completing the circuit) basically it connects the one wire to the other, sending a ground trigger thru the circuit to turn on the lights. Sometimes these wires will get corroded, or the metal contact surface of the plunger switch will oxidize and you'll get intermittent issues. These plunger switches are serviceable, and can be "reconditioned" with ease. They are usually just threaded into the door jamb and can be backed out and cleaned.
Now...there is the possibility that this "switch" is inside the door lock/latch mechanism, which makes this procedure much more difficult. But I know at least on the late model XJ's (97-01) they are just plunger switches that are screwed into the door jamb. If broken, Junk Yard replacements are super inexpensive, or universal replacements can be purchased at any car audio store.