【问题标题】:XSLT 1.0 a potential number of replacements with REPLACE method produce a StackOverFlow ExceptionXSLT 1.0 使用 REPLACE 方法的潜在替换数量会产生 StackOverFlow 异常
【发布时间】:2016-07-19 22:25:45
【问题描述】:

我尝试使用 XSLT 1.0 版将 XML 文件转换为 Json 格式,我正在使用 XSL 上的 Replace 方法进行潜在的替换。这会产生 StackOverFlowException。

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>

【问题讨论】:

  • 您使用哪种 XSLT 1.0 处理器?
  • 我没有使用任何 XSLT 1.0 特殊处理器,我是使用 XSLT 的新手
  • 通过您的处理器运行home.arcor.de/martin.honnen/xslt/processorTest2.xml 并告诉我们结果。或者继续使用 Saxon 9、Altova 或 XmlPrime 提供的 XSLT 2.0。
  • 您能否提供一个发生此行为的示例 XML?以及最初调用模板的方式(以确定您传递给参数的内容)。

标签: xml xslt xml-parsing xslt-1.0


【解决方案1】:

当您使用 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(., '&quot;', '\&quot;')"/>
</xsl:template>

</xsl:stylesheet>

&lt;data&gt;This is a text with "quoted" parts&lt;/data&gt; 转换为&lt;data&gt;This is a text with \"quoted\" parts&lt;/data&gt;

作为替代方案,xsltproc(使用 libxslt 的命令行工具)有一个设置 --maxdepth val : increase the maximum depth (default 3000),如果您需要更多递归深度,我认为您可能需要增加/更改。

【讨论】:

  • 我正在使用 libxml 和 c++,你能告诉我如何增加 maxdepth 值吗,谢谢
  • 恐怕我对libxslt的C API细节不熟悉,需要其他人帮忙。
  • 我查看了xsltproc 的来源,并在git.gnome.org/browse/libxslt/tree/xsltproc/… 中设置了ctxt-&gt;maxTemplateDepthxsltTransformContextPtr
猜你喜欢
  • 1970-01-01
  • 2020-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-10
  • 2011-04-15
  • 2011-02-19
相关资源
最近更新 更多