XSLT 副本和字符 8211

XSLT copy-of and character 8211

我正在使用 xslt 2.0 并使用 copy-of 来复制大部分 XML。

我有这个XML(有问题的部分):

<nitf>
<body>
<table class="4-col">
<tr><td>Sarpsborg &#8211; Høvik</td><td>6</td><td>-</td><td>8</td>
</tr>
</table>
</body>
</nitf>

这是 XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:output indent="yes" media-type="text/xml" method="xml" encoding="ISO-8859-15" />

<xsl:template name="createBody">
    <xsl:copy-of select="/nitf/body"  />
</xsl:template>

输出是:

<nitf>
<body>
    <table class="4-col">
        <tr>
           <td>Sarpsborg &#x2013; Høvik</td>
           <td>6</td>
           <td>-</td>
           <td>8</td>
        </tr>
    </table>
</body>
</nitf>

预期的输出应该是:

<nitf>
<body>
    <table class="4-col">
        <tr>
           <td>Sarpsborg &#8211; Høvik</td>
           <td>6</td>
           <td>-</td>
           <td>8</td>
        </tr>
    </table>
</body>
</nitf>

解决方法

感谢 Martin Honnen,我能够通过添加对一些撒克逊扩展的引用来获得正确的输出。因为我们有商业许可证,所以我能够使用这个扩展。 您可以通过将 saxon 命名空间添加到样式表根节点来添加它:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:saxon="http://saxon.sf.net/"
    exclude-result-prefixes="xs"
    version="2.0">

然后将以下内容添加到 xsl:output 元素:saxon:character-representation="decimal" 你的输出应该是这样的(或者至少我的是):

<xsl:output indent="yes" encoding="ISO-8859-15" saxon:character-representation="decimal" />

Martin Honnen 在他的回答中链接到这个扩展,所以请仔细阅读这个扩展的工作原理。

XSLT 处理器使用 XML 解析器将您的输入 XML 解析为具有 Unicode 字符的节点树。该树根本不包含任何字符引用,而只包含字符。如果将文本节点复制到输出并将输出序列化为文件,XSLT 处理器将序列化文本节点并根据序列化规则和编码的需要转义任何字符。无法在所选输出编码中表示的 Unicode 字符将根据需要进行转义,但编码(即十六进制或十进制)的选择取决于 XSLT 处理器。

如果您使用 Saxon 的商业版本,请参阅 http://saxonica.com/html/documentation/extensions/output-extras/serialization-parameters.html 并尝试设置 <xsl:output saxon:character-representation="decimal" xmlns:saxon="http://saxon.sf.net/"/>,以强制使用十进制表示法。

如果您真的需要保留字符引用,那么您需要预处理 XML,例如使用 LexEv http://andrewjwelch.com/lexev/,将它们变成您可以处理的标记。