Converting Unicode-escaped characters with XSLT

只愿长相守 提交于 2019-12-11 03:38:04

问题


Can anyone say how I convert Unicode code point escaped characters like \u00e4 to the real character ö in XSLT?

I do have...

<text>Eine Repr\u00e4sentation des Objektes geh\u00f6rt...<text>

...and I like to have:

<text>Eine Repräsentation des Objektes gehört...<text>

回答1:


What a fun thing to do... so here's a XSLT 2.0 solution that I came up with:

<?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"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math" 
    exclude-result-prefixes="xs math f"
    xmlns:f="func" version="2.0">

    <xsl:template match="text">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="text/text()">
        <xsl:value-of select="f:unescapeCharachters(.)"/>
    </xsl:template>

    <xsl:function name="f:unescapeCharachters">
        <xsl:param name="text" as="xs:string"/>
        <xsl:analyze-string select="$text" regex="\\u([0-9|abcdefABCDEF]{{4}})">
            <xsl:matching-substring>
                <xsl:value-of select="codepoints-to-string(f:hex-to-dec(regex-group(1)))"/>
            </xsl:matching-substring>
            <xsl:non-matching-substring>
                <xsl:value-of select="."/>
            </xsl:non-matching-substring>
        </xsl:analyze-string>
    </xsl:function>

    <xsl:function name="f:hex-to-dec">
        <xsl:param name="hex"/>
        <xsl:variable name="hexvals" select="'0123456789ABCDEF'"/>
        <xsl:choose>
            <xsl:when test="$hex=''">0</xsl:when>
            <xsl:otherwise>
                <xsl:value-of
                    select="string-length(substring-before($hexvals,substring(upper-case($hex),1,1)))
                * math:pow(16,string-length($hex)-1) + f:hex-to-dec(substring($hex,2))"
                />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:function>

</xsl:stylesheet>


来源:https://stackoverflow.com/questions/26490738/converting-unicode-escaped-characters-with-xslt

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