如何删除或转义 XSL 中 XML 字符串中的新换行符?

发布于 2024-10-16 10:05:08 字数 724 浏览 2 评论 0原文

我有一个 ASP 多行文本框,可将用户定义的文本保存到数据库中。当我检索数据时,我将其序列化为 xml,然后通过 XSL 转换运行它以输出 HTML。

在我的转换中,我通过元素的 onclick 事件将文本框定义的数据传递到 JavaScript 函数中。

我遇到的问题...当用户在文本框中输入回车并将其保存到数据库时,页面加载时会生成一个 JavaScript 错误。

我使用 .NET 的 XslCompiledTransform 进行转换。 XmlDocument 上有一个名为 PreserveWhiteSpace 的属性,默认值为 false,可以设置该属性以去除 XML 中的空白。这解决了不允许用户输入中断文本的问题,但是,客户端希望尽可能保留他们输入的文本的格式。

据我所知,.NET XslCompiledTransform 将回车换行符转换为 

。我相信这些字符正在破坏 javascript。

我的第一个想法是在将字符串传递到 javascript 函数之前去掉 xsl 中的回车符,但我无法弄清楚要“搜索”字符串的哪些字符。

我想另一个问题是 SQL 中存储哪些字符用于 ASP.NET 文本框控件中的回车?

直接查看数据库中的数据,SQL似乎将字符显示为“不可显示”字符或(2个空框)。

有人有过此类事情的经验吗?

I've got an ASP multiline textbox that saves user defined text to a database. When I retrieve the data, I serialize it into xml and then run it through an XSL transform to output my HTML.

Within my transform, I am passing the textbox defined data into a javascript function via an onclick event of an element.

The problem I'm running into...when a user enters a carriage return into the textbox and saves it to the database, a javascript error is generated on page load.

I'm using .NET's XslCompiledTransform to do the transform. There is a property on XmlDocument called PreserveWhiteSpace, default is false, that can be set to strip out white space in the XML. This solves the problem of not allowing a user to enter breaking text, however, the client wants to preserve the formatting of the text that they enter if at all possible.

From what I know, .NET XslCompiledTransform transforms carriage returns-new line into . I believe these are the characters that are breaking the javascript.

My first thought was to strip out the carriage returns within the xsl prior to passing the string into the javascript function, but I've not been able to figure out what characters to "search" the string for.

I guess another question is what characters get stored in SQL for carriage returns from within an ASP.NET textbox control?

Looking directly at the data in the database, SQL seems to display the character as "non-displayable" characters or (2 empty boxes).

Has anyone had any experience with this type of thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等待圉鍢 2024-10-23 10:05:08

我能够在后面的代码中执行此操作以获得我想要的结果:

using (StringWriter sWriter = new StringWriter())
        {

            xTrans.Transform(xDoc, xslArgs, sWriter);

            return sWriter.ToString().Replace("
", "\\r\\n");
        }

I was able to do this in the code behind to get my desired results:

using (StringWriter sWriter = new StringWriter())
        {

            xTrans.Transform(xDoc, xslArgs, sWriter);

            return sWriter.ToString().Replace("
", "\\r\\n");
        }
计㈡愣 2024-10-23 10:05:08

我偶然发现的另一件事......

最初,我想找到一个不需要“编译”代码更改的解决方案,即。在 xsl 中执行此操作的方法又称为“短期修复”。

我首先尝试了这个,但没有成功...

<xsl:variable name="comment" select="normalize-space(.\Comment)" />

这基本上没有任何作用,而且我仍然收到 javascript 错误。

最终,我尝试了这个...

<div onclick="Show('{normalize-space($comment)}'"></div>

第二个实际上可以去除空白,因此避免了 javascript 错误。这并不是满足我的要求的完整解决方案,因为它只解决了 javascript 错误的问题,但是,它可以有效地防止用户“破坏”页面。

因此,它足以作为短期解决方案。

One other thing that I've stumbled across...

Initially, I wanted to find a solution to this problem that did not require a "compiled" code change, ie. a way to do this within xsl aka "a short term fix".

I tried this first and was not successful...

<xsl:variable name="comment" select="normalize-space(.\Comment)" />

This essentially did nothing and I was still receiving the javascript error.

Eventually, I tried this...

<div onclick="Show('{normalize-space($comment)}'"></div>

The second actually worked in stripping out the white space, thus, the javascript error was avoided. This wasn't a complete solution for my requirements because it only solved the issue of the javascript error, however, it would effectively prevent the user from "breaking" the page.

For that reason, it could suffice as a short term solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文