XSLTProcessor::transformToUri(): Memory allocation failed : reaching arbitrary MAX_URI_LENGTH limit

1.2k Views Asked by At

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.

1

There are 1 best solutions below

0
On

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.