XSLT字符串替换

时间:2010-06-18 03:54:17

标签: xslt xpath replace xslt-1.0 xpath-1.0

我真的不知道XSL但是我需要修复这个代码,我已经减少它以使其更简单 我收到此错误

  

无效的XSLT / XPath功能

在这一行

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

这是XSL

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

有谁能告诉我它有什么问题?

6 个答案:

答案 0 :(得分:131)

replace不适用于XSLT 1.0。

编码有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')"/>

另外,请注意,在此示例中,我将变量名称更改为“newtext”,在XSLT变量中是不可变的,因此您不能像在原始代码中那样执行$foo = $foo的等效操作。< / p>

答案 1 :(得分:35)

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

此模板具有以下3个参数

文字: - 您的主要字符串

替换: - 您要替换的字符串

by : - 将以新字符串

回复的字符串

以下是模板

<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了解详细信息。

答案 2 :(得分:12)

注意:如果您希望在需要替换源字符串中的大量实例的情况下使用已经提到的算法(例如,长文本中的新行),则需要概率,由于递归调用,你最终会得StackOverflowException

我解决了这个问题,感谢 Xalan (不知道如何在 Saxon 中进行内置的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>

答案 3 :(得分:6)

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

确保将命名空间xmlns:msxsl="urn:schemas-microsoft-com:xslt"添加到根xsl:stylesheetxsl: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')" />

答案 4 :(得分:1)

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

  1. 将exslt字符串名称添加到样式表中,即:
<xsl:stylesheet
  version="1.0"
  xmlns:str="http://exslt.org/strings"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  1. 然后像这样使用它:
<xsl:value-of select="str:replace(., ' ', '')"/>

答案 5 :(得分:0)

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

谢谢!

相关问题