views:

368

answers:

2

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.

A: 

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' />
Richard
+1  A: 

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" />
Tomalak
Thanks. How do I write out <xsl:call-template name="do-some-string-mainpulation">to escape quotes, new lines since it will be passed to Javascript eventually.
Iris
@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.
Dimitre Novatchev
@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.
Tomalak
The issue I am having is <xsl:variable name="temp"><xsl:apply-templates select="node/nodeset"/></xsl:variable>is empty for when node contains images and other tags. it works fine for plain text.
Iris
@Iris: There is no way to help you unless you provide a minimal (!) sample of your code that shows your problem.
Tomalak