php - XSLTProcessor::transformToUri():内存分配失败:达到任意 MAX_URI_LENGTH 限制
问题描述
我有一些 XML 文件需要在 Html 中“转换”并显示在屏幕上。
我开发了一个简单的脚本,它几乎在所有时间都可以使用,使用DOMDocument
and 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:我尝试用 替换transformToURI
,transformToXML
没有结果。libxml_get_errors()
也没有显示任何结果。
解决方案
您需要增加 PHP 允许在脚本中使用的内存量。首先打开您的php.ini
文件。如果您不知道它的位置,php --ini
请在终端中运行,或者查找标题为的行Loaded Configuration File
并进行编辑(您可能需要 sudo 访问权限)。
找到变量memory_limit
并将其更改为更大的值。(我将我的从 128M 更改为 512M)。
请注意这样做的潜在后果:您可能会遇到内存不足的问题。
推荐阅读
- python - mongo insert_one 给出错误,但插入有效(仅弃用警告)
- angular - 我需要一些帮助才能在 Angular 中编写嵌套的 ngFor 循环
- express - 如何在没有传输编码的情况下代理数据:分块?
- amazon-cloudwatch - 在 AWS Log Insights 图表中将空 bin 显示为零值
- r - 我在 3 个不同的时间点收集了 7 个不同的病毒浓度数据点。如何用 R 中的误差线绘制它?
- ansible - Ansible Tower/AWX 错误?作业任务串行而不是并行运行
- html - 如何使用选择器(无 js)定位特定 div
- javascript - 为什么这个函数中有这些括号 setFormData({ ...formData, [e.target.name]: e.target.value});?
- lua - 在 IDA 函数地址上作弊引擎调试断点
- python - Django 编辑表单不会在带有用户字段的 form.save() 上验证