substring replace on an xml element value
I have a node that looks something like t开发者_StackOverflow中文版his:
<EmailBody> <p>Ticket {$Tckt_Cd$}</p> </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.
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
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;p<gt;Ticket {$Tckt_Cd$}<lt;/p<gt </EmailBody>
</root>
Produces this result:
<root>
<EmailBody> <lt;p<gt;Ticket ololo<lt;/p<gt </EmailBody>
</root>
精彩评论