<xsl:stylesheet xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0" exclude-result-prefixes="xhtml xsl xs">

<xsl:output method="html" indent="yes" encoding="utf-8" omit-xml-declaration="no" version="3.2" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" doctype-system="http://www.w3.org/TR/html4/loose.dtd" />

<xsl:key name="key-anchor-names" match="@name[parent::xhtml:a]" use="." />

<!-- identity template -->
<xsl:template match="@*|node()">
  
<xsl:copy>
    
<xsl:apply-templates select="@*|node()" />
  
</xsl:copy>
</xsl:template>

<!-- hrefs -->
<xsl:template match="@href[parent::xhtml:a]">
 
<xsl:variable name="anchor-text" select="substring(., 2)" />
  
  
<xsl:attribute name="href">#<xsl:value-of select="generate-id(key('key-anchor-names', $anchor-text))" /></xsl:attribute>
</xsl:template>

<!-- anchors -->
<xsl:template match="@name[parent::xhtml:a]">
  
<xsl:attribute name="name"><xsl:value-of select="generate-id(.)" /></xsl:attribute>
</xsl:template>

<xsl:template match="@id[parent::xhtml:a]" />

</xsl:stylesheet>