XSLT 转换后,HTML 实体无法在浏览器中正确呈现

发布于 2024-08-15 04:23:23 字数 3737 浏览 6 评论 0原文

我有以下 XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<example>
    <contactInfo>
        <id>12319221</id>
        <name>Jerry P</name>
        <market>
            <name>Test</name>
            <phone>800.555.1010</phone>
        </market>
        <agent>
            <name>Test User</name>
            <email>[email protected]</email>
        </agent>
        <summary>&amp;#8220;Jerry just gets it!&amp;#8221;</summary>
    </contactInfo>
</example>

当我保存此 xml 文档时,我将特殊字符编码为 html 实体,因此智能引号如何编码为 &#8220;和&#8221。

我通过 Java/Xalan 使用 XSL 将 xml 文档转换为 html:

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

<xsl:template match="/showcase">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Example</title>
        </head>
        <body>
            <div id="profile">
                <xsl:apply-templates/>
            </div>
        </body>
    </html>
</xsl:template>

<!-- Contact Info section -->
<xsl:template match="/example/contactInfo">
    <span class="sectionTitle">Contact Info:</span>
    <div id="contactInfo">
        <xsl:if test="name">
            <strong>Candidate Name:</strong>&#160;<xsl:value-of disable-output-escaping="yes" select="name" /><br />
        </xsl:if>

        <xsl:if test="id">
            <strong>Candidate ID:</strong>&#160;<xsl:value-of disable-output-escaping="yes" select="id" /><br />
        </xsl:if>

        <xsl:if test="market">
            <xsl:if test="market/name">
                <strong>Market Name:</strong>&#160;<xsl:value-of disable-output-escaping="yes" select="market/name" /><br />
            </xsl:if>

            <xsl:if test="market/phone">
                <strong>Market Phone:</strong>&#160;<xsl:value-of disable-output-escaping="yes" select="market/phone" /><br />
            </xsl:if>
        </xsl:if>

        <xsl:if test="agent">
            <xsl:if test="agent/name">
                <strong>Agent Name:</strong>&#160;<xsl:value-of disable-output-escaping="yes" select="agent/name" /><br />
            </xsl:if>

            <xsl:if test="agent/email">
                <strong>Agent Email:</strong>&#160;<xsl:value-of disable-output-escaping="yes" select="agent/email" /><br />
            </xsl:if>
        </xsl:if>

        <xsl:if test="summary">
                <strong>Summary:</strong>&#160;<xsl:value-of disable-output-escaping="yes" select="summary" /><br />
        </xsl:if>
    </div>
    <hr size="1" noshade="noshade" class="rule" />
</xsl:template>
</xsl:stylesheet>

然后将转换产生的 html 写入浏览器。这是我注意到字符编码问题的地方。的(nbsp 数值)显示为黑色菱形问号 (firefox) 或方框字符 (ie),之前编码的实体 (“/”) 也是如此。

另外,也许最大的提示是,在 Linux 平台上转换此 xml 文件(然后将 html 写入 firefox)时,一切都会正确显示。只有当从 Windows 完成转换时才会出现字符编码问题(在 Firefox 和 IE 中)。

我是否错误地对实体进行了编码,或者可能没有在某处指定字符集?

I have the following XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<example>
    <contactInfo>
        <id>12319221</id>
        <name>Jerry P</name>
        <market>
            <name>Test</name>
            <phone>800.555.1010</phone>
        </market>
        <agent>
            <name>Test User</name>
            <email>[email protected]</email>
        </agent>
        <summary>&#8220;Jerry just gets it!&#8221;</summary>
    </contactInfo>
</example>

I am encoding special characters as html entities when I save this xml document, hence how the smart quotes are encoded as “ and ”.

And I use an XSL, via Java/Xalan, to transform the xml document to html:

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

<xsl:template match="/showcase">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Example</title>
        </head>
        <body>
            <div id="profile">
                <xsl:apply-templates/>
            </div>
        </body>
    </html>
</xsl:template>

<!-- Contact Info section -->
<xsl:template match="/example/contactInfo">
    <span class="sectionTitle">Contact Info:</span>
    <div id="contactInfo">
        <xsl:if test="name">
            <strong>Candidate Name:</strong> <xsl:value-of disable-output-escaping="yes" select="name" /><br />
        </xsl:if>

        <xsl:if test="id">
            <strong>Candidate ID:</strong> <xsl:value-of disable-output-escaping="yes" select="id" /><br />
        </xsl:if>

        <xsl:if test="market">
            <xsl:if test="market/name">
                <strong>Market Name:</strong> <xsl:value-of disable-output-escaping="yes" select="market/name" /><br />
            </xsl:if>

            <xsl:if test="market/phone">
                <strong>Market Phone:</strong> <xsl:value-of disable-output-escaping="yes" select="market/phone" /><br />
            </xsl:if>
        </xsl:if>

        <xsl:if test="agent">
            <xsl:if test="agent/name">
                <strong>Agent Name:</strong> <xsl:value-of disable-output-escaping="yes" select="agent/name" /><br />
            </xsl:if>

            <xsl:if test="agent/email">
                <strong>Agent Email:</strong> <xsl:value-of disable-output-escaping="yes" select="agent/email" /><br />
            </xsl:if>
        </xsl:if>

        <xsl:if test="summary">
                <strong>Summary:</strong> <xsl:value-of disable-output-escaping="yes" select="summary" /><br />
        </xsl:if>
    </div>
    <hr size="1" noshade="noshade" class="rule" />
</xsl:template>
</xsl:stylesheet>

The html that results from the transform is then written to the browser. Here is where I'm noticing a character encoding issue. The   (nbsp numeric value) show up as either black diamond question marks (firefox) or a box character (ie) and so do the entities that were previously encoded (“ / ”).

Also, maybe the biggest hint of all is that when transforming this xml file on a linux platform (then writing html to firefox) everything appears correctly. It's only when the transform is done from windows do the character encoding issues occur (in both firefox and ie).

Am I encoding the entities incorrectly or maybe not specify a character set somewhere?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悸初 2024-08-22 04:23:23

你说你正在使用Java/Xalan。您是否提供输出流或流编写器?如果是这样,您需要在此时显式设置编码:

... new OutputStreamWriter(stream,"UTF-8");

仅包含 UTF8 标头实际上不会导致输出文件采用 UTF8 编码。

You say you are using Java/Xalan. Are you prividing the output stream or stream writer? If so you need to explicitly set the encoding at that point:

... new OutputStreamWriter(stream,"UTF-8");

Just including the UTF8 headers does not actually cause the output file to be UTF8 encoded.

迷你仙 2024-08-22 04:23:23

好吧,您还没有在 HTML 文档中设置编码。不知道这是否是问题所在,但这将是我第一次尝试解决。

尝试添加:

到您的头部。

Well you havent set the encodeing in the HTML document for one. Dont know if thats the issue but that would be my first attempt to fix.

try adding:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

to your head.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文