我在使用 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 …