在变量中插入内容

时间:2011-04-27 14:30:46

标签: xml xslt

我将整个事物存储在变量中。当我收到这个时,我需要将路径附加到我想要的令牌。 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

在上面的内容中,我想在WasToken的任何地方追加路径,所以在追加路径之后它必须成为

WasToken=AAECAzREQjgyNTY3NERCODI2Q0Zjbj1zYmswMi9vdT1iaG5zbS9vdT1pc2hzZy9vdT1kaXZhZG1pbi9vPWRvaANcWzW6mcn5IzHXtLr+BuYEMPTX; path=/; domain=.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

如何使用xsl执行此操作。

1 个答案:

答案 0 :(得分:0)

XSLT在递归和字符串操作方面不是很好,这需要两者,但它是可能的。这是我的解决方案(如果您可以确定某些字符串模式(例如,如果“WasToken”始终位于字符串的开头),则可能有更多不必要的条件)):

<?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>