尝试运行XSL模板时出现错误
问题描述:
在获得提示后:xslt string replace concrete example, 我已将这两个模板用于我自己的环境中。 他们使用的HTML表,以便从另一个的.xsl模板调用第二个模板(FHB-围兜,资讯),当得到一些格式化完成:尝试运行XSL模板时出现错误
<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
答
与同事,我设法解决我的问题和调试我的代码,使我的模板现在被通缉的工作的帮助。我的逻辑中的关键缺陷是我期望在替换操作之后必须特别显示bib-info元素。我发现情况并非如此。
另一件事是需要对替换操作进行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>
我发现,我在我的脚本所做的错误! ''可能不是''-loop的一部分,因为它是一个类似于'-call 。 –
直到现在我才知道。 任何人都可以帮助我重写脚本,使“replace-action”适合于“外部可调用的模板”吗?我不知道如何描述它。 –