0

我有一些 XML 文件需要在 Html 中“转换”并显示在屏幕上。

我开发了一个简单的脚本,它几乎在所有时间都可以使用,使用DOMDocumentand XSLTProcessor.

问题是有时它会给出这个错误,而生成的 html 只是完整内容的一部分:

XSLTProcessor::transformToUri(): Memory allocation failed : reaching arbitrary MAX_URI_LENGTH limit in /var/www/test/index.php on line 14

这是我的脚本的工作副本,它对相同的文件给出了相同的错误。

<?php
$xslPath = 'test.xsl';
$xmlString = file_get_contents('test.xml');

$xml = new DOMDocument;
$xml->loadXML($xmlString);

$xsl = new DOMDocument;
$xsl->load($xslPath);

$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);

$proc->transformToURI($xml, 'php://output');

我试图将输出保存到文件中,但我仍然遇到同样的错误,所以php://output应该不是问题。我该如何解决这个问题?

编辑: 看起来问题出在以下代码中。事实上,如果我删除以下行,我将不再看到问题。我希望这有帮助:

<a name="link" href="data:{$mimeType}/{$format};base64,{normalize-space(Attachment)}" download="{$attachmentName}">
    <xsl:value-of select="attachmentName" />
</a>

附件本身是一个 base64 pdf 文件(在本例中是一个 ~1mb 字符串,但可能更多)

编辑 2:如果我尝试使用命令行xsltproc命令生成 html,会发生这种情况:

xsltproc --stringparam target cora_cmd test.xsl test.xml > test.html
URI error : Memory allocation failed : reaching arbitrary MAX_URI_LENGTH limit
URI error : Memory allocation failed : escaping URI value

编辑 3:我尝试用 替换transformToURItransformToXML没有结果。libxml_get_errors()也没有显示任何结果。

4

1 回答 1

0

您需要增加 PHP 允许在脚本中使用的内存量。首先打开您的php.ini文件。如果您不知道它的位置,php --ini请在终端中运行,或者查找标题为的行Loaded Configuration File并进行编辑(您可能需要 sudo 访问权限)。

找到变量memory_limit并将其更改为更大的值。(我将我的从 128M 更改为 512M)。

请注意这样做的潜在后果:您可能会遇到内存不足的问题。

于 2021-02-06T00:33:21.690 回答