I understand this question might be beyond Saxon and more related to the architecture of the application using it for transformations, but just wanted to give a try. Consider the following files-
XML
<?xml version="1.0" encoding="UTF-8"?>
<document>
string
</document>
XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xsl xs">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="node()">
<xsl:apply-templates select="."/>
</xsl:template>
</xsl:stylesheet>
The XSL will go into an infinite recursion during the transformation aka stack overflow. My question is- Is there a way to stop or prevent this type of transformation from going into an infinite recursion? Any parameters that can be added to the command-line that can trigger a warning and gracefully stop?
Instead of relying on existing settings to address this sort of thing, you might just want to create your own.
Consider the following XSL run against the very simple XML you gave:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xsl xs">
<xsl:variable name="recursion.limit" select="500" as="xs:integer"/>
<xsl:variable name="new.line" select="'
'" as="xs:string"/>
<xsl:template match="/">
<xsl:value-of select="$new.line"/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="node()">
<xsl:param name="recursion.count" select="1" as="xs:integer"/>
<xsl:choose>
<xsl:when test="$recursion.count <= $recursion.limit">
<xsl:value-of select="'<' || name() || '>' || ':' || $recursion.count || $new.line" disable-output-escaping="yes"/>
<xsl:apply-templates select=".">
<xsl:with-param name="recursion.count" select="$recursion.count + 1" as="xs:integer"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:message>
<xsl:value-of select="'Recursion limit of ' || $recursion.limit|| ' hit.'"/>
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
This would certainly stop or prevent this type of transformation from going into an infinite recursion, but it's of course not automatic. You have to set it up in code. But if done, this can function as a home-brew max-depth setting. All you would have to do at this point is parameterize the sheet to take such a value instead of baking it in as I did, and there's your setting. And that takes care of your desire for parameters that can be added to the command line that can trigger some sort of warning and/or gracefully stop. It's pure XSL, and as such, ought to be engine-independent, provided the XSL spec is being properly met by your engine of choice (which I really do hope is Saxon).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With