【问题标题】:XSLT replacing <span with new line -> <spanXSLT 用新行替换 <span -> <span
【发布时间】:2017-03-04 06:49:27
【问题描述】:

我在 xslt 中有这段代码,

    <xsl:template match="/">
                    // some truncated js code here
                    for (var i=0 ; i &lt; arrIdentifiers.length ; i++) { 
                        $("#list").append('<li class="ui-widget-content"><span class="sort-handle">&#9776;</span>'+arrIdentifiers[i]+'</li>');
                        }
                    // some truncated js code here
</xsl:template>

由于某种原因,输出换行了 (看看 span 是如何换行的)

这导致了一个 JS 错误..

                    for (var i=0 ; i &lt; arrIdentifiers.length ; i++) { 
                        $("#list").append('<li class="ui-widget-content">
                        <span class="sort-handle">&#9776;</span>'+arrIdentifiers[i]+'</li>');
                        }

无论如何我可以指示模板不处理内部 html?

【问题讨论】:

  • 把内容放到CDATA:&lt;xsl:template match="/"&gt;&lt;![CDATA[ ... text here ... ]]&gt;&lt;/xsl:template&gt;

标签: java xslt xerces


【解决方案1】:

您没有说您使用的是哪个 XSLT 处理器(关键字 xerces 肯定是无关紧要的)。您也没有说您的 xsl:output 选项是什么,但我怀疑您明确或隐含地使用了 method=html 和 indent=yes。

使用这些序列化选项,允许序列化程序在特定上下文中添加空格(包括换行符)。不允许将其添加到脚本元素中:不清楚您的代码是否最终出现在脚本元素中。但是如果你打算以这种方式在 Javascript 文字中包含 HTML 元素,你就会遇到这样的问题。将其转义或使用纯 Javascript DOM 方法(或 jQuery 或其他方法)会更安全。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 2015-09-29
    相关资源
    最近更新 更多