Suppose we wish to replace 27th CD with:
<cd27 pos27="Armstrong">...</cd27> New element/attribute dependent on input
<xsl:template match="cdlist">
<xsl:copy>
<xsl:for-each select="cd">
<xsl:sort select="artist" />
<xsl:element name="{concat('cd',string(position()))}">
<xsl:attribute name= "concat('pos' , string(position()))"
<xsl:value-of select="title" />
</xsl:attribute>
<xsl:apply-templates />
</xsl:element>
</xsl:for-each>
</xsl:copy>
</xsl:template>