xml - 在 XSLT 转换中保留 """实体

标签 xml xslt

我有以下 XML:

<doc>
    <chap>
        &gt;The bowler &gt;delivers&lt; the ball &lt;
        &gt;to the batsman who attempts to &lt;
        &gt;hit the ball &quot; with his  &quot;  bat away from &lt;
        &gt;the fielders so he can run to the &lt;
        &gt;other end of the pitch and score a run.&lt;
    </chap>
</doc>

我必须编写一些 XSL 来对此输入 XML 执行一些任务。但是当我使用复制所有节点时...

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

...在结果文件中,而不是 "。在生成的文件中,这些内容被转换为 "。我不希望这种情况发生。

<doc>
    <chap>
        &gt;The bowler &gt;delivers&lt; the ball &lt;
        &gt;to the batsman who attempts to &lt;
        &gt;hit the ball " with his  "  bat away from &lt;
        &gt;the fielders so he can run to the &lt;
        &gt;other end of the pitch and score a run.&lt;
    </chap>
</doc>

是否有任何方法可以将 " 保留在生成的 XML 中?

最佳答案

您可以使用xsl:use-character-maps,如下所示:

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

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

    <xsl:character-map name="CharMap">
        <xsl:output-character character="&quot;" string="&amp;quot;"/>
    </xsl:character-map>

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

关于xml - 在 XSLT 转换中保留 "&quot;"实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32323872/

相关文章:

c# - 如何使用 C# 从 XML 中提取标签到 XLS

java - 写入 xml 文件导致的 Jar FileNotFoundException

xml - XSLT 按子元素排序和分组

xslt - XSLT 中的字符串比较

xml - 在 XSLT 中保留属性新行

c# - 通用属性的序列化

ios - 从对应的父元素获取子元素的数据

xml - golang XML 以 'invalid UTF-8' 错误结束解析

xml - 检查属性是否存在以及其值非空

css - XSLT与CSS显示XML