Sunday, May 1, 2011

Manipulate the output of <xsl:apply-templates>

How do I string manipulation on the output of <xsl:apply-templates>?

I need to escape quotes etc as it's finally being passed to a JavaScript variable.

From stackoverflow
  • Capture in a variable, then manipulate the value to create the output:

    <xsl:variable name='temp'>
      <xsl:apply-templates ...>
    </xsl:variable>
    
    <xsl:value-of select='expression involving $temp' />
    
  • The nice and clean approach would be to change your XSLT so that it's output does not need any additional manipulation.

    If your call to <xsl:apply-templates> produces a string that must be manipulated for some reason, you would need to capture it in a variable first:

    <xsl:variable name="temp">
      <xsl:apply-templates />
    </xsl:variable>
    
    <xsl:variable name="temp-manipulated">
      <xsl:call-template name="do-some-string-mainpulation">
        <xsl:with-param name="string" select="$temp" />
      </xsl:call-template>
    </xsl:variable>
    
    <xsl:value-of select="$temp-manipulated" />
    

    Alternatively, you can integrate the <xsl:apply-templates> into the <xsl:with-param>, wich would spare you one step:

    <xsl:variable name="temp">
      <xsl:call-template name="do-some-string-mainpulation">
        <xsl:with-param name="string">
          <xsl:apply-templates />
        </xsl:with-param>
      </xsl:call-template>
    </xsl:variable>
    
    <xsl:value-of select="$temp" />
    
    Iris : Thanks. How do I write out to escape quotes, new lines since it will be passed to Javascript eventually.
    Dimitre Novatchev : @Iris This is something not explained at all in your question. Please submit another question asking exactly this. Provide a good example of the input (XML document or just a string) and the wanted output, with explanation about the requirements on the processing.
    Tomalak : @Iris: What prevents you from taking the "nice and clean" route I talked about? I should think this is going to be easier than writing a string manipulation routine.
    Iris : The issue I am having is is empty for when node contains images and other tags. it works fine for plain text.
    Tomalak : @Iris: There is no way to help you unless you provide a minimal (!) sample of your code that shows your problem.

0 comments:

Post a Comment