XSLT string replace

Naprawdę nie znam XSL, ale muszę naprawić ten kod, zmniejszyłem go, aby było prościej.
Dostaję ten błąd

Nieprawidłowa funkcja XSLT / XPath

On this line

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

To jest 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>
Czy ktoś może mi powiedzieć, co jest z nim nie tak?
Author: Abel, 2010-06-18

5 answers

replace nie jest dostępny dla XSLT 1.0.

Kod ma szablon dla string-replace możesz użyć jako substytut funkcji:

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

Wywołany jako:

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

Z drugiej strony, jeśli dosłownie wystarczy zamienić jeden znak na inny, możesz wywołać translate który ma podobny podpis. Coś takiego powinno działać dobrze:

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

Zauważ również, że w tym przykładzie zmieniłem nazwę zmiennej na "newtext", w zmiennych XSLT są niezmienne, więc nie możesz zrobić odpowiednika $foo = $foo, jak w oryginalnym kodzie.

 127
Author: Mark Elliot,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-08 21:16:42

Oto funkcja XSLT, która będzie działać podobnie do ciągu znaków.Replace () funkcji C#.

Ten szablon ma 3 parametry jak poniżej

Text: - twój główny ciąg

Replace : - łańcuch, który chcesz zastąpić

By: - łańcuch, który odpowie nowym łańcuchem

Poniżej szablon

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

Poniżej przykład pokazuje jak to nazwać

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

Możesz również odnieść się do poniżej URL do szczegółów.

 33
Author: Optimus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-22 14:00:47

Uwaga: w przypadku, gdy chcesz użyć wspomnianego już algo w przypadkach, w których musisz zastąpić ogromną liczbę instancji w łańcuchu źródłowym (np. nowe linie w długim tekście), istnieje wysokie prawdopodobieństwo, że skończysz z StackOverflowException z powodu wywołania rekurencyjnego.

Rozwiązałem ten problem dzięki Xalan 's (nie patrzyłem jak to zrobić w Saxon ) wbudowanemu osadzaniu typu 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>
 11
Author: Milan Aleksić,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-11 07:37:32

Możesz użyć poniższego kodu, gdy twój procesor działa na. net lub używa MSXML(w przeciwieństwie do procesorów Java lub innych natywnych). Wykorzystuje msxsl:script.

Upewnij się, że dodałeś przestrzeń nazw xmlns:msxsl="urn:schemas-microsoft-com:xslt" do głównego elementu xsl:stylesheet lub xsl:transform.

Dodatkowo, bind outlet do dowolnej przestrzeni nazw, na przykład 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
Author: John Jin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-24 23:53:00

Rouine jest całkiem dobry, jednak powoduje, że moja aplikacja się zawiesza, więc musiałem dodać sprawę:

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

Zanim funkcja zostanie wywołana rekurencyjnie.

I got the answer from here: gdy test wisi w nieskończonej pętli

Dziękuję!
 0
Author: Chesare,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 10:31:24