* Santander to buy 8 pct Bank of Shanghai stake from HSBC
* Deal, including cooperation agreement, worth 470 mln eur
* Bucking the trend as big rivals sell out of China
* Santander says eyeing China trade flows with Latam
MADRID/LONDON, Dec 10 (Reuters) - Santander, Spain's largest bank, is to buy HSBC's 8 percent stake in Bank of Shanghai, just as many international rivals are beginning to sell out of China.
Santander, which already has a consumer finance venture in China as well as a car financing business, said on Tuesday the Bank of Shanghai deal also included a cooperation agreement, taking the value of its investment to 470 million euros ($647.3 million).
Several major U.S. and European banks including Bank of America and Switzerland's UBS have started shedding their Chinese holdings for a variety of regulatory and business reasons.
HSBC, meanwhile, has been selling minority holdings as part of a restructuring since the start of 2011. It has cut 46,000 jobs and sold or closed 52 businesses, including a minority stake in Chinese insurer Ping An.
But Santander said it would help state-controlled Bank of Shanghai, which it said had 98 billion euros of assets and was the second-biggest city-focused commercial and retail bank in the country, with training in areas such as risk management.
It is also planning to develop a joint wholesale banking business, adding to what Santander might be able to offer some of its big corporate clients in Latin America, a key hub that provides around 50 percent of its profits.
"Santander is also developing investment banking activities in China, mainly based on financing the substantial trade flows between the Asian giant and Latin America," the bank said in a statement which listed existing ventures in China.
Asia-focused HSBC said China was still one of its priority markets and Chief Executive Stuart Gulliver has said in recent months that its 19 percent stake in Bank of Communications remained core.
"Our priorities ... will emphasise the growth of our own operations in mainland China and our own partnership with Bank of Communications," Peter Wong, chief executive of HSBC Asia Pacific, said in a statement on Tuesday.
Gulliver had said in May that HSBC could sell its Bank of Shanghai stake for between $500 million and $600 million. Santander declined to comment on Tuesday on how much it had paid for the 8 percent stake alone.
Santander also has 20 percent of Bank of Beijing's consumer finance subsidiary and earlier this year launched a 50/50 joint venture in car financing with Anhui Jianghuai Automobile (JAC) .
Peers have been selling out of China in part because some struggled to make quick progress with their Chinese joint ventures, and also because they are bulking up capital ahead of more stringent international solvency rules.
Santander's main rival in Spain, BBVA, recently trimmed its stake in China's CITIC Bank Corp to just under 10 percent, as having stakes in foreign banks will become more expensive over that limit under new capital regulations.
Santander has sold assets elsewhere in recent years, including in Latin America. But it has been in a more acquisitive mode in recent months, even in its home market Spain, which is slowly emerging from a five-year economic slump.
In October it spent 140 million euros on a 51 percent stake in the country's largest consumer finance business, run by department store chain El Corte Ingles.
Santander said it expected its deal with Bank of Shanghai to close in the first half of 2014. It said the transaction would impact its capital to risk-weighted assets ratio by about 0.01 percent.
Bank of Shanghai was reported earlier this year to be planning to list its shares in Hong Kong within the next 12 months, though it was not immediately clear if the Santander deal would influence those plans.