XSLT函数用于在一定数量的单词后添加逗号(,)

XSLT函数用于在一定数量的单词后添加逗号(,),xslt,xslt-2.0,Xslt,Xslt 2.0,我遇到了一个问题,需要在字符串中的某些单词后添加逗号。例如,字符串是: 我的名字叫约翰·史密斯,我有一只蝙蝠,它的价格是150美元 我叫迈克尔·道森,我有一辆自行车,它的价格是10000美元 转化为 我叫约翰·史密斯,我有一只狗,它的价格是100美元 我的名字叫迈克尔·道森,我有一辆自行车,它的价格是10000美元 我想我们需要使用字符串长度来计算空格,但是如何用逗号替换第5/9空格之后的空格呢 请建议 谢谢您可以定义一个命名模板在指定位置添加字符,在指定位置插入一个字符,如下所示: <?

我遇到了一个问题,需要在字符串中的某些单词后添加逗号。例如,字符串是:

我的名字叫约翰·史密斯,我有一只蝙蝠,它的价格是150美元

我叫迈克尔·道森,我有一辆自行车,它的价格是10000美元

转化为

我叫约翰·史密斯,我有一只狗,它的价格是100美元

我的名字叫迈克尔·道森,我有一辆自行车,它的价格是10000美元

我想我们需要使用
字符串长度来计算空格,但是如何用逗号替换第5/9空格之后的空格呢

请建议


谢谢

您可以定义一个命名模板
在指定位置添加字符
,在指定位置插入一个字符,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />

  <xsl:template match="data">
    <result>
      <xsl:for-each select="string">
        <string>
          <xsl:call-template name="add_character_at_position">
            <xsl:with-param name="string" select="."/>
            <xsl:with-param name="position" select="@pos"/>
          </xsl:call-template>
        </string>
      </xsl:for-each>
    </result>
  </xsl:template>

  <xsl:template name="add_character_at_position_recurse">
    <xsl:param name="prefix"/>
    <xsl:param name="suffix"/>
    <xsl:param name="position"/>
    <xsl:param name="char"/>
    <xsl:param name="seperator"/>

    <xsl:choose>

      <xsl:when test="$position = 0">
        <xsl:value-of select="concat($prefix, $char, $suffix)"/>
      </xsl:when>

      <xsl:otherwise>
        <xsl:call-template name="add_character_at_position_recurse">
          <xsl:with-param name="prefix" select="concat($prefix, $seperator, substring-before(substring($suffix, 2), $seperator))"/>
          <xsl:with-param name="suffix" select="concat($seperator, substring-after(substring($suffix,2), $seperator))"/>
          <xsl:with-param name="position" select="$position - 1"/>
          <xsl:with-param name="char" select="$char"/>
          <xsl:with-param name="seperator" select="$seperator"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>

  </xsl:template>

  <xsl:template name="add_character_at_position">
    <xsl:param name="string"/>
    <xsl:param name="position"/>
    <xsl:param name="char" select="','"/>
    <xsl:param name="seperator" select="' '"/>

    <xsl:variable name="result">
      <xsl:call-template name="add_character_at_position_recurse">
        <xsl:with-param name="prefix" select="''"/>
        <xsl:with-param name="suffix" select="concat($seperator, $string)"/>
        <xsl:with-param name="position" select="$position"/>
      <xsl:with-param name="char" select="$char"/>
      <xsl:with-param name="seperator" select="$seperator"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:value-of select="substring($result, 2)"/>

  </xsl:template>

</xsl:stylesheet>
生成以下结果:

<?xml version="1.0" encoding="UTF-8"?>
<result>
  <string>My name is John Smith, I have a bat its costs is $150</string>
</result>

我叫约翰·史密斯,我有一只球棒,它的价格是150美元

谢谢马库斯,非常感谢你的回复

我找到了另一种方法,使用tokenize。然后再次运行for循环,在特定的字数后放置分隔符

        <xsl:for-each select="tokenize(text,' ')">
            <word>
                <xsl:value-of select="normalize-space(.)"/>
            </word>
        </xsl:for-each>


将逗号放在语法上合理的位置并不是一件小事(例如,
我的全名是John,Smith,我有一个蝙蝠…
)想法是找出一种方法,在一定数量的单词后添加一些内容,在这种情况下忽略语法。好的。这稍微容易一些,但是
tokenize()
不是XSLT1.0的一部分。:-)
        <xsl:for-each select="tokenize(text,' ')">
            <word>
                <xsl:value-of select="normalize-space(.)"/>
            </word>
        </xsl:for-each>