【问题标题】:Change HTML content inside a node with XSL 1.0使用 XSL 1.0 更改节点内的 HTML 内容
【发布时间】:2013-10-10 08:44:53
【问题描述】:

我想更改 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>

我想将“[[”替换为“&amp;lt;em&amp;gt;”,将“]]”替换为“&lt;/em&gt;”。

我的 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>

问题:“&amp;lt;em&amp;gt;”实际上是“&amp;lt;em&amp;gt;”。 如何强制 XSL 保留 CDATA 中定义的字符?

谢谢!

【问题讨论】:

    标签: html xml xslt


    【解决方案1】:

    您的代码不容易理解。替换实际上发生在 的某个地方 - 这里使用 代替。

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

    一样简单
    <xsl:template match="...">
      <em>  
        <xsl:value-of select="$text"/>
      </em>  
    </xsl:template>
    

    【讨论】:

    • 感谢您的回答。重要的部分在xsl:template match="Content"。它采用“内容”节点并替换所有“[[”和“]]”,然后打印 2 替换的结果。我尝试在&lt;xsl:value-of select="$text" /&gt; 中添加disable-output-escaping="yes",但没有任何改变......
    • 真的很奇怪!在尝试 IE(6) 后,它正在工作...... Firefox 24 会是问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    相关资源
    最近更新 更多