Inserting something in variable
I have the whole thing stored in variable. when I receive this, I need to append the path to token which token I wish. path is path=/; domain=.myorg.com ;
WasToken=AAECAzREQjgyNTY3NERCODI2Q0Zjbj1zYmswMi9vdT1iaG5zbS9vdT1pc2hzZy9vdT1kaXZhZG1pbi9vPWRvaANcWzW6mcn5IzHXtLr+BuYEMPTX; sessiontoken=DstTDbgX0ydK/nAixEvkBQCHrZ5CAar2DGq55U0Mo/6ll1+XEad3/cWyeXdO6hwmBePreLIW5d4CaoxiZHMEwuI5faG1j0UeRNizNJAw8JqEI0HuhpdpY5qSO46Iz+8WffmQoyemD/eQnWMkCeJbMwMHGIebSh7Q9mfC7wqoMqr+28IoeOYM93pPqVVwTiGOXEmiWdcp1S6WTKyw17E06S0GDlLA8eZlFFxFksgGtQiqsmp2vevWvz2dpmYYUPZ6TK3+vsNjkV2urGj24DHIrFGgU5J2eJq9qNEdEAtsuH71TJ6R0pyVK6h4FpaT1p5KurkdAZlIOl/Rpuzw2rnDNVJgW4PzWkeKIjoDDhlUhZ2cM4nNBoHqMGCz2HkwwbU72QVTzzXgwhOXtjrUlaaoZA==; WasToken=AAECAzREQjgyNTY3NERCODI2Q0Zjbj1zYmswMi9vdT1iaG5zbS9vdT1pc2hzZy9vdT1kaXZhZG1pbi9vPWRvaANcWzW6mcn5IzHXtLr+BuYEMPTX; NonceToken=61AF4E4A72A6749A8525787E0071880F; spikerAcceptLanguages=en-us%2Cen%3Bq%3D0.5; spikerStringLocale=en; spikerStyleLocale=en
In the above thing I want to append path to WasToken wherever it is repeating so after appending path it must become
WasToken=AAECAzREQjgyNTY3NERCODI2Q0Zjbj1zYmswMi9vdT1iaG5zbS9vdT1pc2hzZy9vdT1kaXZhZG1pbi9vPWRvaANcWzW6mcn5IzHXtLr+BuYEMPTX; path=/; domain开发者_JAVA技巧=.myorg.com ; sessiontoken=DstTDbgX0ydK/nAixEvkBQCHrZ5CAar2DGq55U0Mo/6ll1+XEad3/cWyeXdO6hwmBePreLIW5d4CaoxiZHMEwuI5faG1j0UeRNizNJAw8JqEI0HuhpdpY5qSO46Iz+8WffmQoyemD/eQnWMkCeJbMwMHGIebSh7Q9mfC7wqoMqr+28IoeOYM93pPqVVwTiGOXEmiWdcp1S6WTKyw17E06S0GDlLA8eZlFFxFksgGtQiqsmp2vevWvz2dpmYYUPZ6TK3+vsNjkV2urGj24DHIrFGgU5J2eJq9qNEdEAtsuH71TJ6R0pyVK6h4FpaT1p5KurkdAZlIOl/Rpuzw2rnDNVJgW4PzWkeKIjoDDhlUhZ2cM4nNBoHqMGCz2HkwwbU72QVTzzXgwhOXtjrUlaaoZA==; WasToken=AAECAzREQjgyNTY3NERCODI2Q0Zjbj1zYmswMi9vdT1iaG5zbS9vdT1pc2hzZy9vdT1kaXZhZG1pbi9vPWRvaANcWzW6mcn5IzHXtLr+BuYEMPTX; path=/; domain=.myorg.com ; NonceToken=61AF4E4A72A6749A8525787E0071880F; spikerAcceptLanguages=en-us%2Cen%3Bq%3D0.5; spikerStringLocale=en; spikerStyleLocale=en
How to do this using xsl.
XSLT is not very good at recursion and string manipulation, and this requires both, but it is possible. Here is my solution (which may have more conditions than necessary if you can be assured of certain string patterns (e.g. if "WasToken" is always at the beginning of the string)):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:variable name="long.string">WasToken=AAECAzREQjgyNTY3NERCODI2Q0Zjbj1zYmswMi9vdT1iaG5zbS9vdT1pc2hzZy9vdT1kaXZhZG1pbi9vPWRvaANcWzW6mcn5IzHXtLr+BuYEMPTX; sessiontoken=DstTDbgX0ydK/nAixEvkBQCHrZ5CAar2DGq55U0Mo/6ll1+XEad3/cWyeXdO6hwmBePreLIW5d4CaoxiZHMEwuI5faG1j0UeRNizNJAw8JqEI0HuhpdpY5qSO46Iz+8WffmQoyemD/eQnWMkCeJbMwMHGIebSh7Q9mfC7wqoMqr+28IoeOYM93pPqVVwTiGOXEmiWdcp1S6WTKyw17E06S0GDlLA8eZlFFxFksgGtQiqsmp2vevWvz2dpmYYUPZ6TK3+vsNjkV2urGj24DHIrFGgU5J2eJq9qNEdEAtsuH71TJ6R0pyVK6h4FpaT1p5KurkdAZlIOl/Rpuzw2rnDNVJgW4PzWkeKIjoDDhlUhZ2cM4nNBoHqMGCz2HkwwbU72QVTzzXgwhOXtjrUlaaoZA==; WasToken=AAECAzREQjgyNTY3NERCODI2Q0Zjbj1zYmswMi9vdT1iaG5zbS9vdT1pc2hzZy9vdT1kaXZhZG1pbi9vPWRvaANcWzW6mcn5IzHXtLr+BuYEMPTX; NonceToken=61AF4E4A72A6749A8525787E0071880F; spikerAcceptLanguages=en-us%2Cen%3Bq%3D0.5; spikerStringLocale=en; spikerStyleLocale=en</xsl:variable>
<xsl:template match="/">
<!-- This template can be called from different places with different params fed into it for the beginning and ending delimiters and the string to be inserted. -->
<xsl:call-template name="convert.string">
<xsl:with-param name="string" select="$long.string" />
<xsl:with-param name="delim.beg">WasToken=</xsl:with-param>
<xsl:with-param name="delim.end">; </xsl:with-param>
<xsl:with-param name="ins.string">path=/; domain=.myorg.com ; </xsl:with-param>
</xsl:call-template>
</xsl:template>
<!-- This is a template that will potentially be called later from itself (recursed) -->
<xsl:template name="convert.string">
<xsl:param name="string" />
<xsl:param name="delim.beg" />
<xsl:param name="delim.end" />
<xsl:param name="ins.string" />
<xsl:choose>
<!-- Do a test using "starts-with" first (instead of just "contains") in case there's a situation where the ending delimiter text precedes the beginning delimiter text. -->
<xsl:when test="starts-with($string, $delim.beg) and contains($string, $delim.end)">
<!-- Output everything up until the first instance of the ending delimiter. -->
<xsl:value-of select="substring($string, 1, string-length(substring-before($string,$delim.end)))" />
<!-- Output the ending delimiter. -->
<xsl:value-of select="$delim.end" />
<!-- Ouput the string to be inserted. -->
<xsl:value-of select="$ins.string" />
<!-- Call this template again but only using the remainder text that follows the ending delimiter. -->
<xsl:call-template name="convert.string">
<xsl:with-param name="string" select="substring-after($string, $delim.end)" />
<xsl:with-param name="delim.beg" select="$delim.beg" />
<xsl:with-param name="delim.end" select="$delim.end" />
<xsl:with-param name="ins.string" select="$ins.string" />
</xsl:call-template>
</xsl:when>
<!-- This test catches text that contains (but doesn't start with) a set of delimiters (perhaps after $string was truncated in a previous pass). It outputs everything in the string up until the first instance of the beginning delimiter. -->
<xsl:when test="contains($string, $delim.beg) and contains(substring-after($string, $delim.beg), $delim.end)">
<xsl:value-of select="substring-before($string, $delim.beg)" />
<xsl:call-template name="convert.string">
<xsl:with-param name="string" select="substring-after($string, $delim.end)" />
<xsl:with-param name="delim.beg" select="$delim.beg" />
<xsl:with-param name="delim.end" select="$delim.end" />
<xsl:with-param name="ins.string" select="$ins.string" />
</xsl:call-template>
</xsl:when>
<!-- Output the remainder text (or all of the text if the delimiters were never found). -->
<xsl:otherwise>
<xsl:value-of select="$string" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
精彩评论