简体   繁体   中英

XSLT1.0 - Replace string in a XML document

I have an XML document and I want to replace some special substrings using XSLT 1.0. I can't use the replace function (it's only available for XSLT 2.0). For this reason I found an alternative solution (the template string-replace-all ) and I'm trying to use it... but with no success. This is an example of XML:

<parent> 
    <child1>hello world!</child1> 
    <child2>example of text</child2> 
</parent>

I want to replace "world" with "guys". I have this xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns="urn:hl7-org:v2xml" xmlns:hl7="urn:hl7-org:v2xml" exclude-result-prefixes="hl7">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

<!--Identity template, copia tutto in uscita -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<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:template match="text()" >
    <xsl:variable name="newtext">
        <xsl:call-template name="string-replace-all">
            <xsl:with-param name="text" select="." />
            <xsl:with-param name="replace" select="world" />
            <xsl:with-param name="by" select="guys" />
        </xsl:call-template>
    </xsl:variable>
</xsl:template>
</xsl:stylesheet>

The output is

<parent>
   <child1/>
   <child2/>
</parent>

The result of the call to string-replace-all is set into the variable newtext which is never used.

Just remove <xsl:variable name="newtext"> and </xsl:variable> from the template match="text()" .

And also look at the answer from @hr_117: If you want to replace the string world by guys you have to put them into ' . Otherwise an element world is searched.

For example:

<xsl:template match="text()" >
    <xsl:call-template name="string-replace-all">
        <xsl:with-param name="text" select="." />
        <xsl:with-param name="replace" select="'world'" />
        <xsl:with-param name="by" select="'guys'" />
    </xsl:call-template>
</xsl:template>

You need to change the template parameter to strings.
Try:

    <xsl:call-template name="string-replace-all">
        <xsl:with-param name="text" select="." />
        <xsl:with-param name="replace" select="'a'" />
        <xsl:with-param name="by" select="'A'" />
    </xsl:call-template>

The select without the single quotation select="a" is looking for an element a .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM