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

时间:2014-10-27 06:18:10

标签: xslt xslt-2.0

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

  

String1:我的名字是约翰史密斯我有一个蝙蝠,其费用是150美元

     

String2:我的名字是Michael Dawson我有一辆自行车,费用是10000美元

转换为

  

String1:我的名字是约翰史密斯,我有一只狗,它的成本是100美元

     

String2:我叫Michael Dawson,我有一辆自行车,费用是10000美元

我认为我们需要使用string-length计算空格,但是如何用逗号替换第5/9空格后的空格?

请建议

由于

2 个答案:

答案 0 :(得分:1)

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

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

它使用递归(在XSLT 1.0中通常)来拆分字符串并在当前prefixsuffix之间插入字符。如果您需要多个字符(逗号),则必须多次嵌套调用此模板。请注意,模板当前不会检查position参数是否有效。

测试设置使用以下输入XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<data>
  <string pos="5">My name is John Smith I have a bat its costs is $150</string>
</data>

生成以下结果:

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

答案 1 :(得分:0)

感谢Marcus,非常感谢您的回复。

我通过使用tokenize找到了另一种方法。然后再次运行循环以将分隔符放在特定数量的单词之后。例如

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