One way is to maintain a dict (a mapping from arbitrary keys to values) of all strings that you have seen before. So, let's assume you are at the point where you have the value in a variable val
, and that there is a dict valdict
that's initially empty. The code you would need is something like this:
import uuid
if val in valdict: # We have seen this reference before
print '<value href="#%s"/>' % valdict[val]
else: # We need to add this reference
valdict[val] = str(uuid.uuid4()).upper()
print '<value id="%s">%s</value>' % (valdict[val], val)
I wouldn't really recommend this simplistic method for forming the XML iself, but it sounds like you are already well-prepared to handle that side of things.