Re: [xsl] Does <xsl:copy> use a lot of memory? Is there an alternative that is more efficient?

Subject: Re: [xsl] Does <xsl:copy> use a lot of memory? Is there an alternative that is more efficient?
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Mon, 03 Sep 2012 16:51:46 +0100
>I agree that is the way to go if there are no recursive references. But if there are recursive references then that approach causes the program goes into an infinite loop. My XML document has lots of complex, indirect recursive references.

I don't quite see how the use of variables will prevent the infinite recursion.

To prevent infinite recursion you need to detect cycles, and the way to do that is to pass on each template call a list of the idref's currently being expanded, and check an idref against that list before you expand it

<xsl:template match="*[@idref]">
<xsl:param name="active" as="xs:string*"/>
<xsl:copy>
<xsl:copy-of select="@* except @idref" />
<xsl:variable name="refed-element" select="key('ids', @idref)"/>
<xsl:if test="exists($refed-element) and not(@idref = $active)">
<xsl:apply-templates select="key('ids', @idref)">
<xsl:with-param name="active" select="($active, @idref)"/>
</xsl:apply-templates>
</xsl:if>
</xsl:copy>
</xsl:template>


Michael Kay
Saxonica

Current Thread