Consider dynamic XSLT where PHP passes multiples of 100 to parse the range of nodes in the source XML into smaller outputs using XSLT's position()
. Specifically, PHP passes a loop variable as a parameter into XSLT binded to $splitnum
(very similar to SQL parameterization).
Input (assuming an XML structure like your previous post)
<root>
<property>
<rent>
<term>short</term>
<freq>week</freq>
<price_peak>5845</price_peak>
<price_high>5845</price_high>
<price_medium>4270</price_medium>
<price_low>3150</price_low>
</rent>
</property>
<property>
<rent>
<term>long</term>
<freq>week</freq>
<price_peak>6845</price_peak>
<price_high>6845</price_high>
<price_medium>4270</price_medium>
<price_low>3150</price_low>
</rent>
</property>
...
</root>
XSLT
(save as .xsl file, a special .xml file; script expects a parameter to be passed in for node range split)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:param name="splitnum" />
<xsl:template match="/root">
<xsl:copy>
<xsl:variable name="currsplit" select="$splitnum - 99"/>
<xsl:apply-templates select="property[position() >= $currsplit and
position() <= $splitnum]" />
</xsl:copy>
</xsl:template>
<xsl:template match="property">
<xsl:copy>
<xsl:copy-of select="*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
PHP
(passes loop iterator variable into XSLT as a parameter; produces 10 XMLs, each with successive 100 property nodes, extend limit of 1000 as needed)
// Load XML and XSL
$xml = new DOMDocument;
$xml->load('Input.xml');
$xsl = new DOMDocument;
$xsl->load($xslstr);
$prop_total = $xml->getElementsByTagName('property')->length + 100;
for($i=1; $i<=$prop_total; $i++){
if ($i % 100 == 0) {
// Configure transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
// Binds loop variable to XSLT parameter
$proc->setParameter('', 'splitnum', $i);
// Transform XML source
$newXML = new DOMDocument;
$newXML = $proc->transformToXML($xml);
// Output file
file_put_contents('rentals_'.$i.'.xml', $newXML);
}
}