There is no easy way to solve this (I considered implementing this for the calibre editor a long time ago, and decided not to, because it cannot easily be made robust) -- if you use id selectors, your selector can still be trumped by another id selector that is more specific, for example, if it also has a class or a descendant selector and so on (say #xxx.yyy or p#xxx or p > span#xxx)
See
https://css-tricks.com/specifics-on-...le-header-id-0 for a good overview.
What you can do do is go over all rules in all stylesheets, find the highest specificity selector that applies to the element in question, then if:
1) The selector has an id, simply merge the rules from the inline style block into that rule, taking care, in particular of shorthand properties
2) The selector does not have an id -- create a new id based selector by giving the element a random id if it does not already have one
You can refine this process by adding a few more if statements to (2) so that you use class based selectors more often.
And note that even this process is not quite bulletproof there are a few edge cases I have not mentioned -- left as an exercise to the reader.