我遇到了一个奇怪的问题,我确信是编码错误,但在排除错误时,PHP显示了一个奇怪的行为,我希望有人能帮助我理解这个错误。
我有一些通过XQuery生成的xml:
<?xml version="1.0" encoding="UTF-8"?>
<list>
<item>
<orig>London, British Library Harley 2251: <ref target="Quis_Dabit/British_Library_Harley_2251/British_Library_Harley_2251_f42v.html">
<orig xmlns="http://www.tei-c.org/ns/1.0">O alle ye doughtres · of Jerusalem</orig>
</ref>
</orig>
</item>
<item>
<orig>London, British Library Harley 2255: <ref target="Quis_Dabit/British_Library_Harley_2255/British_Library_Harley_2255_f67r.html">
<orig xmlns="http://www.tei-c.org/ns/1.0">
<hi rend="blue_pilcrow">¶</hi>O alle ye douħtren of <hi rend="underline">ierusaleem</hi>
</orig>
</ref>
</orig>
</item>
<item>
<orig>Long Melford, Holy Trinity Church Clopton Chantry Chapel: <ref target="Quis_Dabit/Clopton/ww_qd_2.html">
<orig xmlns="http://www.tei-c.org/ns/1.0">
<hi>O</hi> alle ye <gap quantity="8" unit="chars" reason="illegible"/>s of ierusaleem</orig>
</ref>
</orig>
</item>
<item>
<orig>Cambridge, Jesus College Q.G.8: <ref target="Quis_Dabit/Jesus_College_Q_G_8/Jesus_Q_G_8_f20r.html">
<orig xmlns="http://www.tei-c.org/ns/1.0">
<hi>A</hi>ll the <hi rend="underline">doughtren </hi>of <hi rend="underline">Ierusalem</hi> .</orig>
</ref>
</orig>
</item>
<item>
<orig>Oxford, Bodleian Library Laud 683: <ref target="Quis_Dabit/Laud_683/Laud_683_f78v.html">
<orig xmlns="http://www.tei-c.org/ns/1.0">O alle ẏe douhtren of jerusaleem</orig>
</ref>
</orig>
</item>
<item>
<orig>Oxford, St. John's College 56: <ref target="Quis_Dabit/St_John_56/St_John_56_73v.html">
<orig xmlns="http://www.tei-c.org/ns/1.0">O alle the doughtren / of Jerusalem ؛</orig>
</ref>
</orig>
</item>
</list>然后将其导入php:
$text = exec ("java -cp saxon9he.jar net.sf.saxon.Query -t -q:test.xq");
$xml = new DOMDocument;
$xml->loadXML($text);
$xsl = new DOMDocument;
$xsl->load('comparison.xsl');
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules
echo $proc->transformToXML($xml);并向其附加一个xsl样式表。
<?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="1.0">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:template match="list">
<div class="comparison">
<ul>
<xsl:apply-templates/>
</ul>
</div>
</xsl:template>
<xsl:template match="item">
<li>
<xsl:apply-templates/>
</li>
</xsl:template>
但是,当我这样做时,结果输出的编码在非标准字符上变得很奇怪,如下所示:

我的假设是,这是结果的编码问题,所以我添加了一个print_r语句,向我展示生成的原始xml和DOM树,然后刷新。

我并不怀疑这是一个编码错误,我计划追踪它,但我想知道的是,如果我添加了一个print_r语句,它为什么会正确显示,但如果没有,它就不会显示。我是否应该在php文件中添加一些我没有添加的内容?谢谢!
发布于 2015-09-05 13:07:07
XSLT正在输出以UTF-8编码的HTML,但PHP中没有任何内容表示类似的编码,因此它很可能默认为系统编码,在基于MacOS的计算机上,系统编码可能是MacOS或ISO8859-1。
您之所以在print_r中获得某种可读性的输出,是因为该指令正在尽最大努力使用UTF-8字符串并使用默认编码打印它。
要查看此效果,请在浏览器中通过单击View > Unicode >Unicode强制进行编码(每个浏览器的实际位置不同)。手动切换到Unicode之后,您应该看到正确的文本。
下一步是修复PHP脚本的输出编码。它必须首先指示浏览器,您的页面是UTF-8,而不是ISO-8859-1.This post explains how to set the output encoding with PHP。第二个答案可能还需要强迫PHP在任何和所有输出语句中使用UTF-8。
https://stackoverflow.com/questions/32408007
复制相似问题