上付き文字にする必要があるのが ® のような 1 文字だけの場合は、悪意のあるものを使わずに XML を残すことができ<sup>
ます
<node name="Some text ®"/>
処理中に上付き文字を探します。次のようなテンプレートが役立ちます。
<xsl:template match="node/@name">
<xsl:param name="nameString" select="string()"/>
<!-- We're stepping through the string character by character -->
<xsl:variable name="firstChar" select="substring($nameString,1,1)"/>
<xsl:choose>
<!-- '®' can be extended to be a longer string of single characters
that are meant to be turned into superscript -->
<xsl:when test="contains('®',$firstChar)">
<sup><xsl:value-of select="$firstChar"/></sup>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$firstChar"/>
</xsl:otherwise>
</xsl:choose>
<!-- If we we didn't yet step through the whole string,
chop off the first character and recurse. -->
<xsl:if test="$firstChar!=''">
<xsl:apply-templates select=".">
<xsl:with-param name="nameString" select="substring($nameString,2)"/>
</xsl:apply-templates>
</xsl:if>
</xsl:template>
ただし、このアプローチはあまり効率的ではありません。特に、多数のname
属性や非常に長いname
属性がある場合はなおさらです。アプリケーションのパフォーマンスが重要な場合は、処理時間への影響が正当かどうかをテストすることをお勧めします。