Those who plan to leave the ports must apply for temporary entry permits at the exit-entry border inspection authorities of the corresponding ports
That is the portion that applies to you.
The temporary entry permit ( TWOV) is for flights only.
If you had arrived in CAN, and wanted to take a train to HKG, you would be allowed since you are staying in the same
port.
But you are not staying in the same port - you are flying from Shanghai to CAN, then wanting to take the train.
Can I ask what is the reason for your itinerary? If you just want to visit CAN for a few hours, why not fly in there
directly?