XML ノードの HTML コンテンツを変更して、一部の BBCode を XSL 1.0 の HTML タグに置き換えたいと考えています。
私の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>
" です。CDATA で定義されている文字を XSL に保持させるにはどうすればよいですか?
ありがとうございました !