This can often be waived if you stay airside and have a visa from another first world country. I've done this personally with a US Visa in the UK and France, can't speak to Spain.
While studying in the US, I once booked a flight to visit my relatives in Prague, flying into Heathrow, then flying out from Gatwick 18 hours later, leaving me with a whole day to explore London. It's supposed to be at the immigration agent's discretion, but he was happy to let me through, despite my Armenian passport.
Return leg was slightly more complicated, because the airline staff in Prague refused to check me in, since the flight was to London, and I had no UK visa. After at least half an hour of arguing, they ended up drawing up a waiver for me to sign, and let me on.