如何在XSL中从字符中去除重音符号? [英] How do I strip accents from characters in XSL?
问题描述
我一直在寻找字符,但是找不到等效于 normalize-space的XSL函数。也就是说,我的内容带有重音UNICODE字符,这很好,但是从该内容中,我正在创建一个文件名,我不希望这些重音符号。
I keep looking, but can't find an XSL function that is the equivalent of "normalize-space", for characters. That is, my content has accented UNICODE characters, which is great, but from that content, I'm creating a filename, where I don't want those accents.
因此,是否存在我在忽略或无法正确搜索以轻松处理字符的内容?
So, is there something that I'm overlooking, or not googling properly, to easily process characters?
在XML数据中:
<filename>gri_gonéwiththèw00mitc</filename>
在XSLT样式表中:
<xsl:variable name="file">
<xsl:value-of select="filename"/>
</xsl:variable>
<xsl:value-of select="$file"/>
结果为gri_gonéwiththèw00mitc
results in "gri_gonéwiththèw00mitc"
其中
<xsl:value-of select='replace( normalize-unicode( "$file", "NFKD" ), "[^\\p{ASCII}]", "" )'/>
没有任何结果。
我做什么我的目标是 gri_gonewiththew00mitc
(不带重音符号)
What I'm aiming for is gri_gonewiththew00mitc
(no accents)
我使用的语法错误吗?
推荐答案
在XSLT / XPath 1.0中,如果要将这些带重音符号的字符替换为无重音字符,可以使用 translate()
函数。
In XSLT/XPath 1.0 if you want to replace those accented characters with the unaccented counterpart, you could use translate()
function.
但是,假定您的重读UNICODE字符不是由unicode字符组成。如果是这种情况,则需要使用XPath 2.0 normalize-unicode()
函数。
But, that assumes your "accented UNICODE characters" aren't composed unicode characters. If that were the case, you would need to use XPath 2.0 normalize-unicode()
function.
然后,如果真正的目标是拥有有效的URI,则应使用 en-for-uri()
And, if the real goal is to have a valid URI, you should use encode-for-uri()
更新:示例
translate('gri_gonéwiththèw00mitc','áàâäéèêëíìîïóòôöúùûü','aaaaeeeeiiiioooouuuu')
结果: gri_gonewiththew00mitc
encode-for-uri('gri_gonéwiththèw00mitc')
结果: gri_gon%C3%A9withth%C3%A8w00mitc
正确的表达式由@biziclop提供建议:
Correct expression provide suggest by @biziclop:
replace(normalize-unicode('gri_gonéwiththèw00mitc','NFKD'),'\P{ASCII}','')
结果: gri_gonewiththew00mitc
注意:我在XPath 2.0中,正确的字符类取反是大写的 \P
。
Note: In XPath 2.0, the correct character class negation is with a capital \P
.
这篇关于如何在XSL中从字符中去除重音符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!