0

我想更改 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>”实际上是“ &lt;em&gt;”。如何强制 XSL 保留 CDATA 中定义的字符?

谢谢 !

4

1 回答 1

0

您的代码不容易遵循。替换实际上发生在 <xsl:value-of select="..."/> 的某个地方 - 这里使用 <xsl:value-of select="..." disable-output-escaping="yes"/> 代替。

@Pierre Morin: <em> 标签在插入时可能会被转义,因此 disable-output-escaping 无效。不同的浏览器在处理转换时可能会有所不同,可能会出现各种错误。尝试一个只包含输出的小解决方案,并在它工作时适应它。我建议使用像 SaxonCE(免费客户端 XSLT 1.0 和 2.0 引擎,javascript)或 libexslt(免费服务器端 XSLT 1.0 引擎,用 c 编写,广泛使用,例如 php)之类的转换引擎
另一个提示:尽量不要应用开始和结束不同代码中的标签。XSLT 不是为此而构建的。您可以在不破坏元素的情况下使用元素,但这需要一些时间来了解 XSLT 的概念。它很强大,一旦你得到它。
我不知道这是否符合您的需求,但在 <em> 中放置一个 $text 就像

<xsl:template match="...">
  <em>  
    <xsl:value-of select="$text"/>
  </em>  
</xsl:template>
于 2013-10-10T09:03:47.740 回答