Dear Ludovic,
The error message indicates that the XPath /datas[@site=$site]/data[num=$x]/value return not just a single element, but multiple instead. Could it be that for some values of @site and @num you have multiple datas. This would explain why it sometimes happens and sometimes it does not.
It is hard to tell as we don't have the whole xml data file, but just a snippet. Maybe you could test this and instead of updating your XML simply output /datas[@site=$site]/data[num=$x]/value
You can then check if you have multiple values and then maybe you want to change your xml (if this should be invalid) or you have to change your XQuery (you could do a FLOWR expression to update all data nodes).
Cheers,
Dirk