mercredi 6 mai 2015

XSLT infinite template recursion in PHP

I am using the XSLT processor available in PHP to transform an XML file. When my XML file is a sample file everything is ok but when I try to process a file with something like 1000 lines I have this error:

Warning: XSLTProcessor::transformToXml(): xsltApplyXSLTTemplate: A potential infinite template recursion was detected. You can adjust xsltMaxDepth (--maxdepth) in order to raise the maximum number of nested template calls and variables/params (currently set to 3000)

My xml file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<TEI xmlns="http://ift.tt/1dDT9n4">
  <teiHeader xml:lang="en" />
  <text>
    <body>
      <div type="chapter" n="1">
        <p>
          <s xml:id="e_1">In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.</s>         
        </p>
        <p>
          <s xml:id="e_2">"Whenever you feel like criticizing any one," he told me, "just remember that all the people in this world haven't had the advantages that you've had."</s>
        </p>
        </body>
  </text>
</TEI>

and my XSLT treatment is:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://ift.tt/tCZ8VR" 
xmlns:exsl="http://exslt.org/common"
xmlns:set="http://exslt.org/sets"
xmlns:tei="http://ift.tt/1dDT9n4"
extension-element-prefixes="exsl set">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="tei:div">
    <xsl:call-template name="split-chapter">
        <xsl:with-param name="nodes" select="tei:p/tei:s"/>
    </xsl:call-template>
</xsl:template>

<xsl:template name="split-chapter">
    <xsl:param name="nodes"/>
    <xsl:param name="limit" select="300"/>
    <xsl:param name="remaining-nodes" select="dummy-node"/>
    <!-- 1. Calculate the total length of nodes -->
    <xsl:variable name="lengths">
        <xsl:for-each select="$nodes">
            <length>
                <xsl:value-of select="string-length()" />
            </length>
        </xsl:for-each>
    </xsl:variable>
    <xsl:variable name="total-length" select="sum(exsl:node-set($lengths)/length)" />
    <!-- 2. Process the chapter: -->
    <xsl:choose>
        <!-- If chapter is too long and can be shortened ... -->
        <xsl:when test="$total-length > $limit and count($nodes) > 1">
            <!-- ... try again with one node less. -->
            <xsl:call-template name="split-chapter">
                <xsl:with-param name="nodes" select="$nodes[not(position()=last())]"/>
                <xsl:with-param name="remaining-nodes" select="$remaining-nodes | $nodes[last()]"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <!-- Otherwise create a chapter with the current nodes ... -->
            <div type="chapter" n="{@n}" length="{$total-length}" >
                <!-- ... list the paras participating in this chapter ... -->
                <xsl:for-each select="$nodes/parent::tei:p">
                    <p>
                        <!-- ... and process the nodes still left in each para. -->
                        <xsl:apply-templates select="set:intersection(tei:s, $nodes)"/>
                    </p>
                </xsl:for-each>
            </div>
            <!-- Then process any remaining nodes. -->
            <xsl:if test="$remaining-nodes">
                <xsl:call-template name="split-chapter">
                    <xsl:with-param name="nodes" select="$remaining-nodes"/>
                </xsl:call-template>
            </xsl:if>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

Aucun commentaire:

Enregistrer un commentaire