代码之家  ›  专栏  ›  技术社区  ›  Aximili

XSLT字符串替换

  •  79
  • Aximili  · 技术社区  · 14 年前

    我不太懂XSL,但是我需要修复这个代码,我已经简化了它。

    XSLT/XPath函数无效

    在这条线上

    <xsl:variable name="text" select="replace($text,'a','b')"/>
    

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:inm="http://www.inmagic.com/webpublisher/query" version="1.0">
        <xsl:output method="text" encoding="UTF-8" />
    
        <xsl:preserve-space elements="*" />
        <xsl:template match="text()" />
    
        <xsl:template match="mos">
            <xsl:apply-templates />
    
            <xsl:for-each select="mosObj">
              'Notes or subject' 
               <xsl:call-template
                    name="rem-html">
                    <xsl:with-param name="text" select="SBS_ABSTRACT" />
                </xsl:call-template>
            </xsl:for-each>
        </xsl:template>
    
        <xsl:template name="rem-html">
            <xsl:param name="text" />
            <xsl:variable name="text" select="replace($text, 'a', 'b')" />
        </xsl:template>
    </xsl:stylesheet>
    

    有人能告诉我它怎么了吗?

    5 回复  |  直到 9 年前
        1
  •  151
  •   axel.michel    9 年前

    replace

    Codesling有一个 template for string-replace 您可以使用以下替代函数:

    <xsl:template name="string-replace-all">
        <xsl:param name="text" />
        <xsl:param name="replace" />
        <xsl:param name="by" />
        <xsl:choose>
            <xsl:when test="$text = '' or $replace = ''or not($replace)" >
                <!-- Prevent this routine from hanging -->
                <xsl:value-of select="$text" />
            </xsl:when>
            <xsl:when test="contains($text, $replace)">
                <xsl:value-of select="substring-before($text,$replace)" />
                <xsl:value-of select="$by" />
                <xsl:call-template name="string-replace-all">
                    <xsl:with-param name="text" select="substring-after($text,$replace)" />
                    <xsl:with-param name="replace" select="$replace" />
                    <xsl:with-param name="by" select="$by" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    

    调用为:

    <xsl:variable name="newtext">
        <xsl:call-template name="string-replace-all">
            <xsl:with-param name="text" select="$text" />
            <xsl:with-param name="replace" select="a" />
            <xsl:with-param name="by" select="b" />
        </xsl:call-template>
    </xsl:variable>
    

    另一方面,如果您实际上只需要用另一个字符替换一个字符,您可以调用 translate

    <xsl:variable name="newtext" select="translate($text,'a','b')"/>
    

    $foo = $foo 就像你原来的代码一样。

        2
  •  38
  •   j0k gauthamp    12 年前

    下面是XSLT函数,它的工作方式类似于C的String.Replace()函数。

    此模板有以下3个参数

    :-你的主弦

    :-要替换的字符串

    通过 :-将由新字符串答复的字符串

    下面是模板

    <xsl:template name="string-replace-all">
      <xsl:param name="text" />
      <xsl:param name="replace" />
      <xsl:param name="by" />
      <xsl:choose>
        <xsl:when test="contains($text, $replace)">
          <xsl:value-of select="substring-before($text,$replace)" />
          <xsl:value-of select="$by" />
          <xsl:call-template name="string-replace-all">
            <xsl:with-param name="text" select="substring-after($text,$replace)" />
            <xsl:with-param name="replace" select="$replace" />
            <xsl:with-param name="by" select="$by" />
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$text" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
    

    下面的示例显示了如何调用它

    <xsl:variable name="myVariable ">
      <xsl:call-template name="string-replace-all">
        <xsl:with-param name="text" select="'This is a {old} text'" />
        <xsl:with-param name="replace" select="'{old}'" />
        <xsl:with-param name="by" select="'New'" />
      </xsl:call-template>
    </xsl:variable>
    

    你也可以参考 below URL 关于细节。

        3
  •  13
  •   Milan Aleksić    12 年前

    注: 如果您希望将前面提到的算法用于需要替换源字符串中大量实例(例如,长文本中的新行)的情况,那么 高的 你可能会 StackOverflowException

    我解决这个问题多亏了 (不知道怎么做) 撒克逊人 )内置Java类型嵌入:

    <xsl:stylesheet version="1.0" exclude-result-prefixes="xalan str"
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:xalan="http://xml.apache.org/xalan"
                    xmlns:str="xalan://java.lang.String"
            >
    ...
    <xsl:value-of select="str:replaceAll(
        str:new(text()),
        $search_string,
        $replace_string)"/>
    ...
    </xsl:stylesheet>
    
        4
  •  7
  •   Abel    9 年前

    当处理器在.NET上运行或使用MSXML(与基于Java或其他本机处理器相反)时,可以使用以下代码。它使用 msxsl:script .

    确保添加命名空间 xmlns:msxsl="urn:schemas-microsoft-com:xslt" 为了你的根 xsl:stylesheet xsl:transform 元素。

    outlet 例如,任何你喜欢的名称空间 xmlns:outlet = "http://my.functions"

    <msxsl:script implements-prefix="outlet" language="javascript">
    function replace_str(str_text,str_replace,str_by)
    {
         return str_text.replace(str_replace,str_by);
    }
    </msxsl:script>
    
    
    <xsl:variable name="newtext" select="outlet:replace_str(string(@oldstring),'me','you')" />
    
        5
  •  4
  •   Berend de Boer    5 年前

    我一直在回答这个问题。但是没有一个列出xsltproc的最简单解决方案(可能还有大多数XSLT1.0处理器):

    1. 将exslt strings名称添加到样式表中,即:
    <xsl:stylesheet
      version="1.0"
      xmlns:str="http://exslt.org/strings"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:value-of select="str:replace(., ' ', '')"/>
    
        6
  •  0
  •   Community CDub    8 年前

    rouine相当不错,但是它会导致我的应用程序挂起,所以我需要添加一个案例:

      <xsl:when test="$text = '' or $replace = ''or not($replace)" >
        <xsl:value-of select="$text" />
        <!-- Prevent thsi routine from hanging -->
      </xsl:when>
    

    在递归调用函数之前。

    When test hanging in an infinite loop

    谢谢您!