How to fix a special character in XSLT

ぐ巨炮叔叔 提交于 2021-01-28 08:52:51

问题


I am dealing with below XML where I need to remove a special character in firstname. é in (Andrés) not sure what is this character is actually called. If I process firstname as is it's failing in the Vendor system

<?xml version="1.0" encoding="UTF-8"?>
<reportentry>
<reportdata>
    <id>12345</id>
    <firstname>Andrés</firstname>
    <lastname>Williams</lastname>
</reportdata>
</reportentry>

I simply tried replace function which is working, below is the code. Not sure is there any better way to deal with it ? any suggestions ?

 <xsl:value-of select="replace($string1, 'é', 'e')"/>

Full code

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">

<xsl:variable name="string1" select="/reportentry/reportdata/firstname"/>
<xsl:variable name="comma" select="','"/>
<xsl:output method="text" omit-xml-declaration="yes"/>

<xsl:template match="/reportentry">

    <xsl:value-of select="reportdata/id"/>
    <xsl:value-of select="$comma"/>
    <xsl:value-of select="replace($string1, 'é', 'e')"/>
    <xsl:value-of select="$comma"/>
    <xsl:value-of select="reportdata/lastname"/>

</xsl:template>
</xsl:stylesheet>

I expected result as 12345,Andres,Williams


回答1:


You can strip most diacritics by using normalize-unicode() to convert the string to decomposed normal form (NFD), and then using replace() to remove all "non-spacing mark" characters (category Mn).

So replace(normalize-unicode(xxx, 'NFD'), '\p{Mn}', '')

Not tested.

But it would be better to modernise the receiving application so it can handle international names...



来源:https://stackoverflow.com/questions/56989053/how-to-fix-a-special-character-in-xslt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!