<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:pxi="http://www.daisy.org/ns/pipeline/xproc/internal" xmlns:d="http://www.daisy.org/ns/pipeline/data" version="2.0" exclude-result-prefixes="#all">
<xsl:variable name="link-nodes" select="('noteref', 'annoref')"/>
<xsl:variable name="target-nodes" select="('note', 'annotation')"/>
<xsl:key name="normalized-links" match="*[@idref]" use="substring-after(@idref, '#')"/>
<xsl:key name="links" match="*[@idref]" use="@idref"/>
<xsl:key name="targets" match="*[@id]" use="@id"/>
<xsl:template match="/" priority="5">
<d:audio-order>
<xsl:apply-templates select="*"/>
</d:audio-order>
</xsl:template>
<xsl:template match="*" priority="1">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="*[@id or starts-with(local-name(), 'level')]" priority="2">
<xsl:copy>
<xsl:copy-of select="@id|@pxi:no-smilref"/>
<xsl:apply-templates select="*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[$link-nodes = local-name()]" priority="3">
<xsl:copy>
<xsl:copy-of select="@id|@idref|@pxi:no-smilref"/>
<xsl:apply-templates select="*"/>
</xsl:copy>
<xsl:if test="key('links', @idref)[1]/@id = @id">
<xsl:variable name="target" select="key('targets', substring-after(@idref, '#'))"/>
<xsl:element name="{local-name($target)}" namespace="{namespace-uri($target)}">
<xsl:copy-of select="$target/(@id|@pxi:no-smilref)"/>
<xsl:apply-templates select="$target/*"/>
</xsl:element>
</xsl:if>
</xsl:template>
<xsl:template match="*[$target-nodes = local-name()]" priority="3">
<xsl:if test="not(key('normalized-links', @id))">
<xsl:copy>
<xsl:copy-of select="@id|@pxi:no-smilref"/>
<xsl:apply-templates select="*"/>
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>