十六进制实体在UTF字符中更改



在我的xml中

输入文件:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <a>a text.</a>
    <b>b &#x2013; text.</b>
</root>

我的XSLT:

<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:output method="xml"/>
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

当前输出:

<root>
    <a>a text.</a>
    <b>b – text.</b>
</root>

所需的输出:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <a>a text.</a>
    <b>b &#x2013; text.</b>
</root>

知道您为什么需要这个,因为不同的技术可能会或可能不符合您的要求。

一种方法是将US-ASCII指定为输出编码。然后,所有非ASCII字符将在输出中以数字实体(字符引用)出现。不幸的是,这也意味着您不能在元素或属性名称中使用非ASCII字符。

将字符映射添加到脚本

<?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:output method="xml" use-character-maps="entity"/>
    <xsl:character-map name="entity">
        <xsl:output-character character="&#x2013;" string="&amp;#x2013;"/>
    </xsl:character-map>
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

输出

<?xml version="1.0" encoding="UTF-8"?>
    <root>
        <a>a text.</a>
        <b>b &#x2013; text.</b>
    </root>

请参阅进程http://xsltransform.hikmatu.com/6qm2e2b

最新更新