Reputation: 1074
I have some XML files that I need to "transform" in Html and display on screen.
I have developed a simple script that works -almost- all of the times, using DOMDocument
and XSLTProcessor
.
The problem is that sometimes it gives this error, and the resulting html is only a part of the complete content:
XSLTProcessor::transformToUri(): Memory allocation failed : reaching arbitrary MAX_URI_LENGTH limit in /var/www/test/index.php on line 14
This is a working copy of my script, which gives the same error with the same files.
<?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');
I have tried to save the output to a file, but still I am having the same error, so php://output
shouldn't be the a problem. How can I solve this issue?
EDIT: It looks like the problem lies in the following code. If fact, if I remove the following lines, I am no longer seeing the issue. I hope this helps:
<a name="link" href="data:{$mimeType}/{$format};base64,{normalize-space(Attachment)}" download="{$attachmentName}">
<xsl:value-of select="attachmentName" />
</a>
The attachment itself is a base64 pdf file (which in this case is a ~1mb string, but it could be even more)
EDIT 2: This is what happens if I try to generate the html using the command line xsltproc
command:
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
EDIT 3: I have tried replacing transformToURI
with transformToXML
, no results. libxml_get_errors()
shows no results too.
Upvotes: 0
Views: 1405
Reputation: 451
You can resolve this memory issue by using the
XMLReader
PHP class instead of
DOMDocument
If you're using the Magento framework, you can use
Laminas\Config\Reader\Xml
instead of
Magento\Framework\Xml\Parser
class.
DOMDocument class will try to load all the XML content to memory at once but XMLReader class will NOT load the content to memory at once. It will read the nodes set by set incrementally.
You can find more info on this one in this article https://medium.com/devops-dev/way-to-read-large-xml-dataset-in-magento-2-using-laminas-config-reader-xml-2b59c936bbcc :)
Cheers!
Upvotes: 0
Reputation: 71
You need to increase the amount of memory PHP is allowed to use in a script. Start by opening your php.ini
file. If you don't know where it is located, run php --ini
in the terminal, or look for the row titled Loaded Configuration File
and edit that (you may need sudo access).
Locate the variable memory_limit
and change it to something larger. (I changed mine from 128M to 512M).
Beware of potential ramifications of doing this: you may run into issues with running out of memory.
Upvotes: 0