使用 XSL 1.0 替换字符串

Jim*_*r88 1 xml xslt xls xslt-1.0

我在使用 XSL 1.0 替换字符串中的字符串时遇到了一些问题。我已经阅读了本网站和其他网站上的各种主题,但我无法弄清楚为什么我的方法不起作用。出于这个问题的目的,我对数据进行了一些简化,但希望您能了解要点。

所以,我有我的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="replaceAString.xsl"?>
<Body>
    <aString>textToReplace</aString>
</Body>
Run Code Online (Sandbox Code Playgroud)

和 XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://w3.org/1999/XSL/Transform">

<xsl:template match="/">
    <h1>
       <xsl:value-of select="$myVar"/>
    </h1>
</xsl:template>

<xsl:variable name="myVar">
    <xsl:call-template name="string-replace-all">
        <xsl:with-param name="text" select="Body/aString/" />
        <xsl:with-param name="replace" select="'textToReplace'" />
        <xsl:with-param name="by" select="'withThisText'" />
    </xsl:call-template>
</xsl:variable>

<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:with-param name="text" select="substring-after($text,$replace)"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text" />
        </xsl:otherwise>
     </xsl:choose>
     </xsl:template>
    </xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

我希望这种转换的结果是出现在 Heading1 HTML 标签中的文本“withThisText”。不幸的是我一无所获。这是我似乎无法弄清楚我的逻辑有什么问题的事情之一。我不是 XML/XSL 专业人士,但也不是完全的新手。虽然我确定现在有人会拉我上来:P

任何帮助将不胜感激!我真的被困在这里:(

非常感谢,

詹姆士

Jim*_*r88 6

是的,我已经整理好了伙计们!!我的替换模板有点不正确,我必须承认,感谢您指出这一点。感谢 @helderdarocha 建议我使用控制台进行故障排除。我这样做了,发现很多样式表甚至无法解析!!这个问题的原因是什么?查看我的 xmlns:xsl 命名空间声明;没有“www。”。更正了这一点并对我的代码进行了更改后,替换功能现在可以正常工作了:)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="**http://www.w3.org/1999/XSL/Transform**">

<xsl:variable name="myVar">
    <xsl:call-template name="string-replace-all">
        <xsl:with-param name="text" select="Body/aString" />
        <xsl:with-param name="replace" select="'textToReplace'" />
        <xsl:with-param name="by" select="'withThisText'" />
    </xsl:call-template>
</xsl:variable>

<xsl:template match="/">
    <h1>
        <xsl:value-of select="$myVar"/>
    </h1>
</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="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:stylesheet>
Run Code Online (Sandbox Code Playgroud)

非常感谢!!

问候,

詹姆士