0

after getting hints here: xslt string replace concrete example, I've made these two templates to use in my own environment. They use html-tables in order to get some formatting done when calling the second template (fhb-bib-info) from yet another .xsl-template:

<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>

But when I try to run them I get the following errors. The two templates are part of the file funcs.xsl. Line 153 is where the xsl:value-of select="$newtext" statement occurs.

Can anybody help me debug/clear these error-messages? Kate

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
  • I've found where I made the error in my script! ` ` may not be part of the ``-loop as it is a analogon to a ``-call. – Katharina Wolkwitz Aug 07 '17 at 13:04
  • I did not know that until just now. Can anybody help me rewrite the script in such a way that the "replace-action" is fitted in a "externally-callable template"? I don't know how else to describe it. – Katharina Wolkwitz Aug 07 '17 at 13:12

1 Answers1

0

With the help of a colleague I managed to solve my problem and debug my code so that my templates now work as wanted. The crucial flaw in my logic was that I expected to have to specifically display the bib-info-element after the replace-action. I found out that's not the case.

Another thing is the need for a recursiv call on the replace-action as only on blanc is being replaced instead of the whole bunch.

So here's the working code:

<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>