我想更改 XML 节点的 HTML 内容,以在 XSL 1.0 中用 HTML 标记替换一些 BBCode。
我的 XML 看起来像这样:
<Article>
<Title>Article test</Title>
<Content>
<![CDATA[<p>Lorem Ipsum :</p><p>"[[Lorem ipsum dolor sit amet]] is a great tool !"</p>]]>
</Content>
</Article>
我想将“[[”替换为“ <em>
”,将“]]”替换为“ </em>
”。
我的 XSL 看起来像这样:
<?xml version="1.0" encoding = "ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"
media-type="text/html"
indent="yes"
omit-xml-declaration="yes"
encoding="ISO-8859-1"
doctype-public="-//W3C//DTD HTML 4.0 Strict//FR"
/>
<xsl:template match="/" >
<html>
<head>
<!--...-->
</head>
<body>
<xsl:for-each select="//Article[1]">
<h2><xsl:value-of select="Title"/></h2>
<div class="content"><xsl:apply-templates select="Content"/></div>
</xsl:for-each>
</body>
</html>
</xsl:template><!-- match=/ -->
<xsl:template match="Content">
<xsl:variable name="normaContenu"><xsl:value-of select="normalize-space(.)" /></xsl:variable>
<xsl:variable name="replacePatternStart"><![CDATA[<em>]]></xsl:variable>
<xsl:variable name="replacePatternEnd"><![CDATA[</em>]]></xsl:variable>
<xsl:variable name="contenuWithStartTags">
<xsl:call-template name="replace">
<xsl:with-param name="text" select="$normaContenu" />
<xsl:with-param name="replace" select="'[['" />
<xsl:with-param name="by" select="string($replacePatternStart)" />
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="replace">
<xsl:with-param name="text" select="string($contenuWithStartTags)" />
<xsl:with-param name="replace" select="']]'" />
<xsl:with-param name="by" select="string($replacePatternEnd)" />
</xsl:call-template>
</xsl:template><!-- match=Content -->
<xsl:template name="replace">
<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="replace">
<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><!-- name=replace -->
</xsl:stylesheet>
问题:“ <em>
”实际上是“ <em>
”。如何强制 XSL 保留 CDATA 中定义的字符?
谢谢 !