<xsl:transform version="1.0" extension-element-prefixes="exsl" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common">
<!---->
<xsl:variable name="almost-unique-uri-namespace-nodes" select="//namespace::*[name()!='xml'][not(.=../preceding::*/namespace::* or .=ancestor::*[position()>1]/namespace::*)]"/>
<!---->
<xsl:variable name="almost-unique-uri-bindings-tree">
<xsl:for-each select="$almost-unique-uri-namespace-nodes">
<binding>
<prefix>
<xsl:choose>
<!---->
<xsl:when test="not(name()) and (//*[namespace-uri()=''] or //@*[namespace-uri()=current()])">
<xsl:variable name="alternate-prefix-candidate" select="//namespace::*[count(.|current())!=1][.=current()][name()!=''][1]"/>
<xsl:choose>
<xsl:when test="$alternate-prefix-candidate">
<xsl:value-of select="name($alternate-prefix-candidate)"/>
</xsl:when>
<xsl:otherwise>
<!---->
<xsl:value-of select="generate-id()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="name()"/>
</xsl:otherwise>
</xsl:choose>
</prefix>
<uri>
<xsl:value-of select="."/>
</uri>
</binding>
</xsl:for-each>
</xsl:variable>
<!---->
<xsl:variable name="unique-uri-bindings" select="exsl:node-set($almost-unique-uri-bindings-tree)/binding[not(uri=preceding::uri)]"/>
<!---->
<xsl:variable name="created-namespace-nodes-tree">
<xsl:for-each select="$unique-uri-bindings">
<xsl:variable name="prefix">
<xsl:choose>
<!---->
<xsl:when test="prefix=preceding::prefix">
<xsl:value-of select="generate-id()"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="prefix"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="maybe-colon">
<xsl:if test="string($prefix)">:</xsl:if>
</xsl:variable>
<xsl:element name="{$prefix}{$maybe-colon}temporary" namespace="{uri}"/>
</xsl:for-each>
</xsl:variable>
<!---->
<xsl:variable name="created-namespace-nodes" select="exsl:node-set($created-namespace-nodes-tree)//namespace::*"/>
<!---->
<xsl:template match="/*">
<xsl:call-template name="copy">
<xsl:with-param name="insert-namespace-declarations" select="true()"/>
</xsl:call-template>
<xsl:call-template name="do-xsl-message-diagnostics"/>
</xsl:template>
<!---->
<xsl:template match="*" name="copy">
<xsl:param name="insert-namespace-declarations"/>
<xsl:variable name="prefix" select="name($created-namespace-nodes[.=namespace-uri(current())])"/>
<xsl:variable name="maybe-colon">
<xsl:if test="$prefix">:</xsl:if>
</xsl:variable>
<xsl:element name="{$prefix}{$maybe-colon}{local-name()}" namespace="{namespace-uri()}">
<xsl:if test="$insert-namespace-declarations">
<xsl:copy-of select="$created-namespace-nodes"/>
</xsl:if>
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<!---->
<xsl:template match="@*">
<xsl:variable name="prefix" select="name($created-namespace-nodes[.=namespace-uri(current())])"/>
<xsl:variable name="maybe-colon">
<xsl:if test="$prefix">:</xsl:if>
</xsl:variable>
<xsl:attribute name="{$prefix}{$maybe-colon}{local-name()}" namespace="{namespace-uri()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<!---->
<xsl:template match="text()|comment()|processing-instruction()">
<xsl:copy/>
</xsl:template>
<!---->
<xsl:template name="do-xsl-message-diagnostics">
<xsl:message>
<diagnostics xml:space="preserve">
<diagnostic name="almost-unique-uri-bindings-tree">
<xsl:copy-of select="$almost-unique-uri-bindings-tree"/>
</diagnostic>
<diagnostic name="unique-uri-bindings">
<xsl:copy-of select="$unique-uri-bindings"/>
</diagnostic>
<diagnostic name="created-namespace-nodes-tree">
<xsl:copy-of select="$created-namespace-nodes-tree"/>
</diagnostic>
</diagnostics>
</xsl:message>
</xsl:template>
</xsl:transform>