【问题标题】:Preserve HTML elements in XSLT 1.0在 XSLT 1.0 中保留 HTML 元素
【发布时间】:2014-12-16 15:27:44
【问题描述】:

给定以下 XML:

<application>
    <documentation>Before text.
        [This|http://google.com] is a link.
        Click [here|http://google.com] for another link. 
        After text.
    </documentation>
</application>

我想用&lt;br/&gt; 标签替换新行并创建链接。结果应生成以下 HTML:

Before text.<br/>
<a href="http://google.com">This</a> is a link.<br/>
Click <a href="http://google.com">here</a> for another link.<br/>
After text.

我有下面的 XSLT 和适当的模板。我的问题是第一个函数添加的&lt;br/&gt; 元素被第二个函数删除了。我尝试过使用身份转换,但我无法理解它。

任何帮助将不胜感激。

我当前的 XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" version="1.0"/>
    <xsl:template match="application">
        <p>
            <xsl:variable name="with_new_lines">
                <xsl:call-template name="replace-newlines">
                    <xsl:with-param name="text" select="documentation"/>
                    <xsl:with-param name="replace" select="'&#xA;'"/>
                    <xsl:with-param name="by" select="'new_line'"/>
                </xsl:call-template>
            </xsl:variable>

            <xsl:variable name="with_links">
                <xsl:call-template name="create-links">
                    <xsl:with-param name="text">
                        <xsl:copy-of select="$with_new_lines"/>
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:variable>
            <xsl:copy-of select="$with_links"/>
        </p>
    </xsl:template>
    <xsl:template name="replace-newlines">
        <xsl:param name="text"/>
        <xsl:choose>
            <xsl:when test="contains($text, '&#xA;')">
                <xsl:copy-of select="substring-before($text,'&#xA;')"/>
                <br/>
                <xsl:call-template name="replace-newlines">
                    <xsl:with-param name="text" select="substring-after($text,'&#xA;')"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="$text"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <!-- Changing [here|http://a.com] to <a href="http://a.com">here</a>and
        [http://a.com] to <a href="http://a.com">http://a.com</a>-->
    <xsl:template match="*" name="create-links">
        <xsl:param name="text" select="."/>
        <xsl:choose>
            <xsl:when test="contains($text, 'http')">
                <xsl:copy-of select="substring-before($text,'[')"/>
                <xsl:variable name="the_link_and_after">
                    <xsl:copy-of select="substring-after($text,'[')"/>
                </xsl:variable>
                <xsl:variable name="the_link">
                    <xsl:copy-of select="substring-before($the_link_and_after,']')"/>
                </xsl:variable>
                <xsl:choose>
                    <xsl:when test="contains($the_link,'|')">
                        <xsl:variable name="the_url">
                            <xsl:copy-of select="substring-after($the_link,'|')"/>
                        </xsl:variable>
                        <a href="{$the_url}">
                            <xsl:copy-of select="substring-before($the_link,'|')"/>
                        </a>
                    </xsl:when>
                    <xsl:otherwise>
                        <a href="{$the_link}">
                            <xsl:copy-of select="$the_link"/>
                        </a>
                    </xsl:otherwise>
                </xsl:choose>
                <!--<xsl:copy-of select="substring-after($text,']')" />-->
                <xsl:call-template name="create-links">
                    <xsl:with-param name="text" select="substring-after($text,']')"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="$text"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

【问题讨论】:

    标签: xslt xslt-1.0


    【解决方案1】:

    您的方法的问题在于,您的每个命名模板都会创建一个 result-tree-fragment,其中包含文本节点和元素的混合(&lt;br&gt;&lt;a&gt;)。当您发送结果以作为字符串由其他模板处理时,只有文本节点在处理后仍然存在。

    我建议您首先将输入标记为作为元素行。然后将每一行依次发送给另一个模板处理:

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:exsl="http://exslt.org/common"
    extension-element-prefixes="exsl">
    <xsl:output method="html" encoding="utf-8"/>
    
    <xsl:template match="application">
        <xsl:variable name="lines">
            <xsl:call-template name="tokenize-lines">
                <xsl:with-param name="text" select="documentation"/>
            </xsl:call-template>
        </xsl:variable>
        <p>
            <xsl:for-each select="exsl:node-set($lines)/line">
                <xsl:call-template name="create-links">
                    <xsl:with-param name="text" select="."/>
                </xsl:call-template>
                <br/>
            </xsl:for-each>
        </p>
    </xsl:template>
    
    <xsl:template name="tokenize-lines">
        <xsl:param name="text"/>
        <xsl:choose>
            <xsl:when test="contains($text, '&#xA;')">
                <line><xsl:copy-of select="substring-before($text,'&#xA;')"/></line>
                <xsl:call-template name="tokenize-lines">
                    <xsl:with-param name="text" select="substring-after($text,'&#xA;')"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <line><xsl:copy-of select="$text"/></line>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template name="create-links">
        <xsl:param name="text"/>
        <xsl:choose>
            <xsl:when test="contains($text, 'http')">
                <xsl:copy-of select="substring-before($text,'[')"/>
                <xsl:variable name="the_link_and_after">
                    <xsl:copy-of select="substring-after($text,'[')"/>
                </xsl:variable>
                <xsl:variable name="the_link">
                    <xsl:copy-of select="substring-before($the_link_and_after,']')"/>
                </xsl:variable>
                <xsl:choose>
                    <xsl:when test="contains($the_link,'|')">
                        <xsl:variable name="the_url">
                            <xsl:copy-of select="substring-after($the_link,'|')"/>
                        </xsl:variable>
                        <a href="{$the_url}">
                            <xsl:copy-of select="substring-before($the_link,'|')"/>
                        </a>
                    </xsl:when>
                    <xsl:otherwise>
                        <a href="{$the_link}">
                            <xsl:copy-of select="$the_link"/>
                        </a>
                    </xsl:otherwise>
                </xsl:choose>
                <!--<xsl:copy-of select="substring-after($text,']')" />-->
                <xsl:call-template name="create-links">
                    <xsl:with-param name="text" select="substring-after($text,']')"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="$text"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    </xsl:stylesheet>
    

    或者,您可以从第一个模板中调用第二个模板,在返回之前分别为每一行:

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" encoding="utf-8"/>
    
    <xsl:template match="application">
        <p>
            <xsl:call-template name="create-lines">
                <xsl:with-param name="text" select="documentation"/>
            </xsl:call-template>
        </p>
    </xsl:template>
    
    <xsl:template name="create-lines">
        <xsl:param name="text"/>
        <xsl:choose>
            <xsl:when test="contains($text, '&#xA;')">
                <xsl:call-template name="create-links">
                    <xsl:with-param name="text" select="substring-before($text,'&#xA;')"/>
                </xsl:call-template>
                <br/>
                <xsl:call-template name="create-lines">
                    <xsl:with-param name="text" select="substring-after($text,'&#xA;')"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="create-links">
                    <xsl:with-param name="text" select="$text"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template name="create-links">
        <xsl:param name="text"/>
        <xsl:choose>
            <xsl:when test="contains($text, 'http')">
                <xsl:copy-of select="substring-before($text,'[')"/>
                <xsl:variable name="the_link_and_after">
                    <xsl:copy-of select="substring-after($text,'[')"/>
                </xsl:variable>
                <xsl:variable name="the_link">
                    <xsl:copy-of select="substring-before($the_link_and_after,']')"/>
                </xsl:variable>
                <xsl:choose>
                    <xsl:when test="contains($the_link,'|')">
                        <xsl:variable name="the_url">
                            <xsl:copy-of select="substring-after($the_link,'|')"/>
                        </xsl:variable>
                        <a href="{$the_url}">
                            <xsl:copy-of select="substring-before($the_link,'|')"/>
                        </a>
                    </xsl:when>
                    <xsl:otherwise>
                        <a href="{$the_link}">
                            <xsl:copy-of select="$the_link"/>
                        </a>
                    </xsl:otherwise>
                </xsl:choose>
                <!--<xsl:copy-of select="substring-after($text,']')" />-->
                <xsl:call-template name="create-links">
                    <xsl:with-param name="text" select="substring-after($text,']')"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="$text"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

    • 我们实现了第二个选项,并且效果很好。谢谢@michael.hor257k
    猜你喜欢
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-03
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多