使用 XSLT 转换 Unicode 转义字符 [英] Converting Unicode-escaped characters with XSLT

查看:52
本文介绍了使用 XSLT 转换 Unicode 转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何将像 \u00e4 这样的 Unicode 代码点转义字符转换为 XSLT 中的实际字符 ö?

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

我确实有...

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

...而且我喜欢:

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

推荐答案

这是一件多么有趣的事情...所以这是我想出的 XSLT 2.0 解决方案:

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>

这篇关于使用 XSLT 转换 Unicode 转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆