xml - 如何使用 xslt 中的 xml 数据源将 unicode 转换为 iso?

标签 xml xslt xslt-1.0 xslt-2.0

需要将 XML 的所有 unicode(~) 从 XML(entities.xml) 转换为 iso(˜),其中所有这些值都被存储......

entities.xml:-

<entities>
<entity iso="eacute" unicode="x00e9"/>
<entity iso="iacute" unicode="x00ed"/>
</entities>

输入:-

<?xml version="1.0" encoding="UTF-8"?>
<chapter>
<title>Chapt&#x00e9;r Tilt&#x00e9;</title>
<body>
    <p>Th&#x00ed;s is t&#x00e9;xt...</p>
    <p>Th&#x00ed;s is t&#x00e9;xt...</p>
    <p>Th&#x00ed;s is t&#x00e9;xt...</p>
    <p>Th&#x00ed;s is t&#x00e9;xt...</p>
    <p>Th&#x00ed;s is t&#x00e9;xt...</p>
</body>    
</chapter>

输出应该是:-

<chapter>
<title>Chapt&eacute;r Tilt&eacute;</title>
<body>
    <p>Th&iacute;s is t&eacute;xt...</p>
    <p>Th&iacute;s is t&eacute;xt...</p>
    <p>Th&iacute;s is t&eacute;xt...</p>
    <p>Th&iacute;s is t&eacute;xt...</p>
    <p>Th&iacute;s is t&eacute;xt...</p>
</body>    

entities.xml 中还有许多其他值,例如......

    <entity iso="nbsp" unicode="x00a0"/>
<entity iso="ordf" unicode="x00aa"/>
<entity iso="ordm" unicode="x00ba"/>
<entity iso="para" unicode="x00b6"/>
<entity iso="plusmn" unicode="x00b1"/>
<entity iso="pound" unicode="x00a3"/>
<entity iso="raquo" unicode="x00bb"/>
<entity iso="reg" unicode="x00ae"/>
<entity iso="sect" unicode="x00a7"/>

最佳答案

您可以使用 xslt-2.0 使用字符映射来实现这一点。

尝试以下样式表:

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

    <xsl:character-map name="CharMap">
        <xsl:output-character character="&#x00ed;" string="&amp;iacute;"/>
        <xsl:output-character character="&#x00e9;" string="&amp;eacute;"/>
    </xsl:character-map>

    <xsl:output indent="yes" use-character-maps="CharMap"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

关于xml - 如何使用 xslt 中的 xml 数据源将 unicode 转换为 iso?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24795860/

相关文章:

xml - Xpath:比较十进制类型的节点属性

.net - .Net 中的递归/反射 XSLT

html - 在 xsl-fo/fop 中嵌入 HTML 段落

jquery - 使用 jQuery 遍历元素属性

php - 在 PHP 中将关联数组转换为 XML

xml - Delphi 7 与 XML 通信

javascript - 如何使用一个 XSLT 文件中的变量到另一个 XSLT 文件

xml - 用 xsl :text? 解释换行符

xml - XSLT 1.0 在同一级别上使用不同的值对多个相同节点进行分组

xslt - 如何使用一些空属性进行 xslt Muenchian 分组?