Search code examples
xsltxsl-fo

apply-templates with substring


I'm trying to achieve the following: A string value that is parsed to a template needs to be shortened. I want to shorten the value and apply it.

   <xsl:template name="replace-strings">
      <xsl:param name="text" />     

(...)

   <xsl:variable name="cleaned_text">
        <xsl:value-of select="substring-before($text, '(~)')" />
     </xsl:variable>          
      
      <xsl:choose>
         <xsl:when test="contains($text,'(~)')">
            <xsl:apply-templates select="$cleaned_text" />
         </xsl:when>              
         <xsl:otherwise>
            <xsl:apply-templates select="." />
         </xsl:otherwise>
      </xsl:choose>

This does not work, giving the error

ERROR http-nio-8080-exec-2 org.apache.fop.fo.FOTreeBuilder - org.xml.sax.SAXParseException; lineNumber: 0; columnNumber: 0; #STRING cannot be converted to NodeList! (translated)

When I apply the $text variable, the code works as intended. How do I shorten the string found in the $text variable without affecting the note structure? Sorry if this doesn't make too much sense, I'm still trying to find my way in this language.


Solution

  • You are trying to apply templates to the string returned by substring-before(). You can only use xsl:apply-templates on nodes (which is what the error message is trying to tell you).

    If you change xsl:apply-templates to xsl:value-of, then the result from the template will be either the shortened string or the string value of the current node.

    (You probably want the string value of $text instead.)