How can I replace a string with an XML element using XSLT 1.0?
Source:
<body>This has a keyword and may have another keyword</body>
Desired output:
<body>This has a <kw>keyword</kw> and may have another <kw>keyword</kw></body>
I have the following template but it only supports replacing the string with text.
<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="replace"/>
<xsl:param name="with"/>
<xsl:choose>
开发者_StackOverflow社区 <xsl:when test="contains($text,$replace)">
<xsl:value-of select="substring-before($text,$replace)"/>
<xsl:value-of select="$with"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="substring-after($text,$replace)"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="with" select="$with"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Any tips would be helpful.
Thanks.
From what you already have, it looks like all that is missing is <xsl:element name="keyword"/> And if the element name is dynamic, use <xsl:element name="{XP}"/> where XP is an XPath expression.
精彩评论