如何从XSL中的字符中去除重音?

LOl*_*ffe 4 xml xslt unicode character-encoding

我一直在寻找,但是对于字符来说,找不到相当于"normalize-space"的XSL函数.也就是说,我的内容重音了UNICODE字符,这很棒,但是根据这些内容,我正在创建一个文件名,我不想要那些重音符号.

那么,有什么东西我可以忽略,或者没有正确搜索,以便轻松处理角色?

在XML数据中:

<filename>gri_gonéwiththèw00mitc</filename>
Run Code Online (Sandbox Code Playgroud)

在XSLT样式表中:

<xsl:variable name="file">
    <xsl:value-of select="filename"/>
</xsl:variable>

<xsl:value-of select="$file"/>
Run Code Online (Sandbox Code Playgroud)

结果"gri_gonéwiththèw00mitc"

哪里

<xsl:value-of select='replace( normalize-unicode( "$file", "NFKD" ), "[^\\p{ASCII}]", "" )'/>
Run Code Online (Sandbox Code Playgroud)

没有结果.

我的目标是gri_gonewiththew00mitc(没有口音)

我使用的语法错了吗?

小智 8

在XSLT/XPath 1.0中,如果要用非重音对象替换那些带重音的字符,可以使用translate()函数.

但是,假设您的"重音UNICODE字符"不是由unicode字符组成的.如果是这种情况,则需要使用XPath 2.0 normalize-unicode()功能.

并且,如果真正的目标是拥有有效的URI,那么您应该使用 encode-for-uri()

更新:示例

translate('gri_gonéwiththèw00mitc','áàâäéèêëíìîïóòôöúùûü','aaaaeeeeiiiioooouuuu')
Run Code Online (Sandbox Code Playgroud)

结果: gri_gonewiththew00mitc

encode-for-uri('gri_gonéwiththèw00mitc')
Run Code Online (Sandbox Code Playgroud)

结果: gri_gon%C3%A9withth%C3%A8w00mitc

正确的表达提供@biziclop的建议:

replace(normalize-unicode('gri_gonéwiththèw00mitc','NFKD'),'\P{ASCII}','')
Run Code Online (Sandbox Code Playgroud)

结果: gri_gonewiththew00mitc

注意:在XPath 2.0中,正确的字符类否定是有一个大写的\P.