使用 XSLT 转换编码

Convert encoding with XSLT

我在 XML 中有以下内容,我正在尝试将其转换为某个输出文件,比如 HTML

是否可以将输出文件中的 & 转换为 &?

<Property>
  <name>Document Title</name>
  <value>Me &amp; you</value>   
</Property>

由于您已将问题标记为 XSLT 2.0,如果您确实想要在 HTML 输出中使用未转义的符号 &,那么您可以使用字符映射:

<?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="html" use-character-maps="m1"/>

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

    <xsl:template match="/">
        <html>
            <body>
                <p>This is a test of the encoding of the <code>&amp;</code> character.</p>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

输出是

<html>
   <body>
      <p>This is a test of the encoding of the <code>&</code> character.
      </p>
   </body>
</html>

但是在 HTML 和 XML 中,符号通常被转义为 &amp; 是有原因的,如果你的 XSLT 处理器在 HTML 输出中转义它,那么它是通常实施 HTML 语法规则。