尝试运行XSL模板时出错

时间:2017-08-07 11:02:09

标签: debugging xslt-1.0

在获得提示之后:xslt string replace concrete example, 我已经将这两个模板用于我自己的环境中。 他们使用html表来从另一个.xsl-template调用第二个模板(fhb-bib-info)时完成一些格式化:

<xsl:template name="string-replace-all">
    <xsl:param name="text" />
    <xsl:param name="replace" />
    <xsl:param name="by" />
    <xsl:choose>
        <xsl:when test="$text = '' or $replace = ''or not($replace)" >
            <!-- Prevent this routine from hanging -->
            <xsl:value-of select="$text" />
        </xsl:when>
        <xsl:when test="contains($text, $replace)">
            <xsl:value-of select="substring-before($text,$replace)" />
            <xsl:value-of select="$by" />
            <xsl:call-template name="string-replace-all">
                <xsl:with-param name="text" select="substring-after($text,$replace)" />
                <xsl:with-param name="replace" select="$replace" />
                <xsl:with-param name="by" select="$by" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>


<xsl:template name="fhb-bib-info">
  <xsl:choose>
    <xsl:when test="./bib-info =''">
      <tr>
        <td colspan="2"><xsl:text>Autor: </xsl:text><xsl:value-of select="./z13-author"/></td>
      </tr>
      <tr>
        <td colspan="2"><xsl:text>Titel: </xsl:text><xsl:value-of select="./z13-title"/></td>
      </tr>
      <tr>
        <td colspan="2"><xsl:text>ISBN/ISSN: </xsl:text><xsl:value-of select="./z13-isbn-issn"/></td>
      </tr>
    </xsl:when>
    <xsl:otherwise>
      <xsl:template match="bib-info">
        <xsl:variable name="newtext">
          <xsl:call-template name="string-replace-all">
            <xsl:with-param name="text" select="text()" />
            <xsl:with-param name="replace" select="']:                           '" />
            <xsl:with-param name="by" select="']: '" />
          </xsl:call-template>
        </xsl:variable>
      </xsl:template>
      <tr>
        <td colspan="2"><bib-info><xsl:value-of select="$newtext" /></bib-info></td>
      </tr>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

但是当我尝试运行它们时,我会收到以下错误。这两个模板是文件funcs.xsl的一部分。第153行是发生select =“$ newtext”语句的xsl:value-。

有人可以帮我调试/清除这些错误消息吗?凯特

Error at xsl:value-of on line 153 of file:/C:/Aleph-22-Test/alephcom/files/SWF50/PrintTemplates/ger/funcs.xsl:
  XPST0008: XPath syntax error at char 8 on line 153 in {$newtext}:
    Variable $newtext has not been declared
Error at xsl:template on line 143 of file:/C:/Aleph-22-Test/alephcom/files/SWF50/PrintTemplates/ger/funcs.xsl:
  XTSE0010: An xsl:otherwise element must not contain an xsl:template element
Error at xsl:template on line 143 of file:/C:/Aleph-22-Test/alephcom/files/SWF50/PrintTemplates/ger/funcs.xsl:
  XTSE0010: Element must be used only at top level of stylesheet

1 个答案:

答案 0 :(得分:0)

在同事的帮助下,我设法解决了我的问题并调试了我的代码,以便我的模板现在按照需要运行。我的逻辑中的关键缺陷是我希望在替换动作之后必须专门显示bib-info-element。我发现事实并非如此。

另一件事是需要对replace-action进行recursiv调用,因为只有替换blanc而不是整个。

所以这是工作代码:

<xsl:template name="plain-fhb-bib-info">
  <xsl:choose>
    <xsl:when test="./bib-info =''">
      <xsl:text>Autor: </xsl:text><xsl:value-of select="./z13-author"/><xsl:call-template name="new-line"/>
      <xsl:text>Titel: </xsl:text><xsl:value-of select="./z13-title"/><xsl:call-template name="new-line"/>
      <xsl:text>ISBN/ISSN: </xsl:text><xsl:value-of select="./z13-isbn-issn"/><xsl:call-template name="new-line"/>
    </xsl:when>
    <xsl:otherwise>
    <xsl:call-template name="plain-string-replace-all-recursive">
      <xsl:with-param name="text" select="./bib-info" />
      <xsl:with-param name="replace" select="']:  '" />
      <xsl:with-param name="by" select="']: '" />
    </xsl:call-template>
    </xsl:otherwise>
   </xsl:choose>
</xsl:template>


<!-- recursively applies plain-string-replace-all until there are no more matches -->
<xsl:template name="plain-string-replace-all-recursive">
  <xsl:param name="text" />
  <xsl:param name="replace" />
  <xsl:param name="by" />
  <xsl:choose>
    <xsl:when test="contains($text, $replace)">
      <xsl:variable name="newtext">
       <xsl:call-template name="plain-string-replace-all">
        <xsl:with-param name="text" select="$text" />
        <xsl:with-param name="replace" select="$replace" />
        <xsl:with-param name="by" select="$by" />
       </xsl:call-template>
      </xsl:variable>
      <xsl:call-template name="plain-string-replace-all-recursive">
        <xsl:with-param name="text" select="$newtext" />
        <xsl:with-param name="replace" select="$replace" />
        <xsl:with-param name="by" select="$by" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$text" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<xsl:template name="plain-string-replace-all">
  <xsl:param name="text" />
  <xsl:param name="replace" />
  <xsl:param name="by" />
  <xsl:choose>
    <xsl:when test="$text = '' or $replace = ''or not($replace)" >
      <xsl:value-of select="$text" />
    </xsl:when>
    <xsl:when test="contains($text, $replace)">
      <xsl:value-of select="substring-before($text,$replace)" />
      <xsl:value-of select="$by" />
      <xsl:call-template name="plain-string-replace-all">
        <xsl:with-param name="text" select="substring-after($text,$replace)" />
        <xsl:with-param name="replace" select="$replace" />
        <xsl:with-param name="by" select="$by" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$text" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>