Xml XSLT 1.0使用REPLACE方法的潜在替换次数会产生StackOverFlow异常
我尝试使用XSLT版本1.0将XML文件转换为Json格式,但在这种情况下,使用XSL上的Replace方法可能会有大量替换。这将产生StackOverFlowException Replace方法是递归调用的,所以我遇到了这个问题。有什么解决办法吗Xml XSLT 1.0使用REPLACE方法的潜在替换次数会产生StackOverFlow异常,xml,xslt,xml-parsing,xslt-1.0,Xml,Xslt,Xml Parsing,Xslt 1.0,我尝试使用XSLT版本1.0将XML文件转换为Json格式,但在这种情况下,使用XSL上的Replace方法可能会有大量替换。这将产生StackOverFlowException Replace方法是递归调用的,所以我遇到了这个问题。有什么解决办法吗 <xsl:template name="replace-string"> <xsl:param name="text"/> <xsl:param name="replace"/>
<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="replace"/>
<xsl:param name="with"/>
<xsl:choose>
<xsl:when test="contains($text,$replace)">
<xsl:value-of select="substring-before($text,$replace)"/>
<xsl:value-of select="$with"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="substring-after($text,$replace)"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="with" select="$with"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
在使用libxslt时,可以使用EXSLT
str:replace
函数,而不是递归模板,即
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
exclude-result-prefixes="str">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="str:replace(., '"', '\"')"/>
</xsl:template>
</xsl:stylesheet>
将这是一个包含“引用”部分的文本
转换为这是一个包含“引用”部分的文本
另一种选择是使用libxslt的命令行工具
xsltproc
,它有一个设置--maxdepth val:增加最大深度(默认值3000)
如果需要更多的递归深度,我想您可能需要增加/更改。您使用哪个XSLT 1.0处理器?我不使用任何XSLT 1.0专用处理器,我是通过您的处理器使用XSLTRun的新手,请告诉我们结果。或者转到Saxon9、Altova或XmlPrime提供的XSLT2.0。您能提供一个发生这种行为的示例XML吗?也就是模板最初被调用的方式(找出你传递给参数的方法)。我使用C++的LBXML,请告诉我如何才能增加Max深度值,谢谢。我不太熟悉LIXSLT的C API的细节。我查看了xsltproc
的源代码,并在其中设置了xslttransfermcontextptr
的ctxt->maxTemplateDepth
。