If you end up using XSLT, you may find the generate-id
function useful for generating ids.
Here s a sort of dummy example using XSLT 1.0:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="element-by-id" match="//*" use="@id"/>
<!-- identity transform: everything as-is... -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- ... except for rewritten id s -->
<xsl:template match="@id">
<xsl:attribute name="id">
<xsl:value-of select="generate-id(..)"/>
</xsl:attribute>
</xsl:template>
<!-- ... and rewritten id references -->
<xsl:template match="@ref">
<xsl:variable name="head" select="substring-before(., url(# )"/>
<xsl:variable name="tail" select="substring-after(., url(# )"/>
<xsl:variable name="idref" select="substring-before($tail, ) )"/>
<xsl:variable name="end" select="substring-after($tail, ) )"/>
<xsl:attribute name="ref">
<xsl:value-of select="concat($head, url(# ,
generate-id(key( element-by-id , $idref)),
) , $end)"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
If you don t like the id s produced by generate-id
(or if you cannot use it for other reasons -- to ensure that you get unique id s all the nodes need to be processed within the same transformation) you can replace the calls to it with some other logic, like adding a suffix.