You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将非标准字符转义为Windows 1252字符集?求XSLT实现方案

Convert Special Characters to Windows-1252 Numeric Entities with XSLT 1.0

Your current XSLT is a basic identity transform—it copies XML nodes and content as-is, but it won’t automatically convert special characters to the &#nnn; numeric entities you want. Since these characters are valid in Windows-1252 encoding, the XSLT processor will output them directly instead of escaping them as entities. To fix this, we need to add a custom text-processing template that maps specific Unicode characters to their corresponding Windows-1252 numeric entities.

Here’s the modified XSLT that achieves your goal:

<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" encoding="Windows-1252"/>
    
    <!-- Identity transform: copy all elements and attributes unchanged -->
    <xsl:template match="*|@*">
        <xsl:copy>
            <xsl:apply-templates select="@*" />
            <xsl:apply-templates />
        </xsl:copy>
    </xsl:template>
    
    <!-- Process text nodes to convert special characters -->
    <xsl:template match="text()">
        <xsl:call-template name="convert-characters">
            <xsl:with-param name="text" select="." />
        </xsl:call-template>
    </xsl:template>
    
    <!-- Recursive template to handle each character individually -->
    <xsl:template name="convert-characters">
        <xsl:param name="text" />
        <xsl:if test="$text != ''">
            <!-- Extract the first character from the text -->
            <xsl:variable name="char" select="substring($text, 1, 1)" />
            
            <!-- Map Unicode characters to Windows-1252 numeric entities -->
            <xsl:choose>
                <xsl:when test="$char = '&#x20AC;'"> <!-- € -->
                    <xsl:text>&#128;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x201A;'"> <!-- ‚ -->
                    <xsl:text>&#130;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x0192;'"> <!-- ƒ -->
                    <xsl:text>&#131;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x201E;'"> <!-- „ -->
                    <xsl:text>&#132;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x2026;'"> <!-- … -->
                    <xsl:text>&#133;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x2020;'"> <!-- † -->
                    <xsl:text>&#134;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x2021;'"> <!-- ‡ -->
                    <xsl:text>&#135;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x02C6;'"> <!-- ˆ -->
                    <xsl:text>&#136;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x2030;'"> <!-- ‰ -->
                    <xsl:text>&#137;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x0160;'"> <!-- Š -->
                    <xsl:text>&#138;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x2039;'"> <!-- ‹ -->
                    <xsl:text>&#139;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x0152;'"> <!-- Œ -->
                    <xsl:text>&#140;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x017D;'"> <!-- Ž -->
                    <xsl:text>&#142;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x2018;'"> <!-- ‘ -->
                    <xsl:text>&#145;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x2019;'"> <!-- ’ -->
                    <xsl:text>&#146;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x201C;'"> <!-- “ -->
                    <xsl:text>&#147;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x201D;'"> <!-- ” -->
                    <xsl:text>&#148;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x2022;'"> <!-- • -->
                    <xsl:text>&#149;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x2013;'"> <!-- – -->
                    <xsl:text>&#150;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x2014;'"> <!-- — -->
                    <xsl:text>&#151;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x02DC;'"> <!-- ˜ -->
                    <xsl:text>&#152;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x2122;'"> <!-- ™ -->
                    <xsl:text>&#153;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x0161;'"> <!-- š -->
                    <xsl:text>&#154;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x203A;'"> <!-- › -->
                    <xsl:text>&#155;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x0153;'"> <!-- œ -->
                    <xsl:text>&#156;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x017E;'"> <!-- ž -->
                    <xsl:text>&#158;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#x0178;'"> <!-- Ÿ -->
                    <xsl:text>&#159;</xsl:text>
                </xsl:when>
                <xsl:when test="$char = '&#xA0;'"> <!-- No-break space -->
                    <xsl:text>&#160;</xsl:text>
                </xsl:when>
                <!-- Keep all other characters as-is -->
                <xsl:otherwise>
                    <xsl:value-of select="$char" />
                </xsl:otherwise>
            </xsl:choose>
            
            <!-- Recursively process the rest of the text -->
            <xsl:call-template name="convert-characters">
                <xsl:with-param name="text" select="substring($text, 2)" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:transform>

How This Works

  1. Identity Transform: The first template copies all elements and attributes exactly as they appear in your input XML—so your document structure stays intact.
  2. Text Node Processing: The second template targets all text nodes and passes their content to the recursive convert-characters template.
  3. Recursive Character Mapping: The convert-characters template processes each character one-by-one:
    • It checks if the character matches one of the special Unicode values from your list.
    • If it matches, it outputs the corresponding Windows-1252 numeric entity (e.g., € becomes &#128;).
    • If it doesn’t match, it outputs the character unchanged.
    • It repeats this process for every character in the text node until all content is processed.

Example Output

When you run your sample XML through this XSLT, the output will look like this (truncated for brevity):

<Recordset>
128 &#128; euro sign
130 &#130; single low-9 quotation mark
131 &#131; Latin small letter f with hook
...
159 &#159; Latin capital letter Y with diaeresis
160 &#160; no-break space
</Recordset>

内容的提问来源于stack exchange,提问作者Tim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:22:22