Omnia9
Omnia9

Reputation: 1573

substring replace on an xml element value

I have a node that looks something like this:

<EmailBody> &lt;p&gt;Ticket {$Tckt_Cd$}&lt;/p&gt </EmailBody>

With XSLT, can it replace the value {$Tckt_Cd$} with a parameter?

I'm not that great with XSLT, so any help would be great.

Upvotes: 0

Views: 1720

Answers (2)

Treemonkey
Treemonkey

Reputation: 2163

you could also do something along the lines of

<xsl:variable name="VarA">
<xsl:value-of select="substring-before(substring-after(EmailBody,'{'),'}')"/>
</xsl:variable>

<xsl:choose>
<xsl:when test="$varA='$Tckt_Cd$'">
your replacement here
</xsl:when>
</xsl:choose>

then if you have different variable names you can change the value fairly easily

Upvotes: 1

Flack
Flack

Reputation: 5892

You should probably go with regular expressions if you have XSLT 2.0 or EXSLT available.

Following code is a pure XSLT 1.0 solution.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="some-param" select=" 'ololo' "/>

<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="EmailBody">
    <xsl:copy>
        <xsl:value-of select="substring-before(., '{$Tckt_Cd$}')"/>
        <xsl:value-of select="$some-param"/>
        <xsl:value-of select="substring-after(., '{$Tckt_Cd$}')"/>
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>

Used with a valid XML input:

<root>
    <EmailBody> &lt;lt;p&lt;gt;Ticket {$Tckt_Cd$}&lt;lt;/p&lt;gt </EmailBody>
</root>

Produces this result:

<root>
    <EmailBody> &lt;lt;p&lt;gt;Ticket ololo&lt;lt;/p&lt;gt </EmailBody>
</root>

Upvotes: 0

Related Questions