How can I apply an xslt template to a string?

nodes, string, xslt, xslt-1.0

Solution

If the aim is code re-use, to use the template in multiple places, then what you could do is give your template a name (in addition to the template match), and give it a default parameter

<xsl:template match="text()" name="kvp-print-single" mode="kvp-print-single">
    <xsl:param name="text" select="." />
    <tr>
        <td colspan="3"><xsl:value-of select="$text"/></td>
    </tr>
</xsl:template>

Then just use xsl:call-template to call it with you concatenated string as a parameter

<xsl:call-template name="kvp-print-single">
   <xsl:with-param name="text" select="concat(haba/hiba:text(), ' - ', huba/baba:text())" />
</xsl:call-template>

Note, the template will still match "text()" nodes in the normal way when matched using xsl:apply-templates.

Problem

Given a template used for building some html around a value, I want to pass in a string, rather than a node set. As an example I want to concat some values and pass that to the template. How can I achieve that sort of thing? ``` <xsl:template match="text()" mode="kvp-print-single"> <tr> <td colspan="3"><xsl:value-of select="."/></td> </tr> </xsl:template> ``` ... ``` <xsl:apply-templates select="concat=(haba/hiba:text(), ' - ', huba/baba:text())" mode="kvp-print-single"/> ``` ErrorMsg: xml or stylesheet file is invalid! Exception: System.Xml.Xsl.XsltException: Expression must evaluate to a node-set.

Original source