How do you publish a transaction which is cancelled if not claimed by a given time? How do you publish a transaction which is cancelled if some output is not claimed by a given time? (ie returning all the spent coins to their inputs or else creating new conditions under which they can be claimed)

Is this possible?
Using the blockchain you can't. If the transaction is in the blockchain with a reasonable number of confirmations it cannot be cancelled nor reversed. In that case only the owner of the recipient wallet can spend the balance. If for any reason the private key of that address is lost, the balance is irreversibly lost.

To reverse the transaction in the manner that you describe you would need to use some off-chain transaction method.