如何在使用ColdFusion函数XMLFormat()时转义HTML字符实体? [英] How can I escape HTML character entities when using ColdFusion function XMLFormat()?
问题描述
我有以下HTML块:
<p>The quick brown fox jumps over the lazy dog — The quick brown fox jumps over the lazy dog.</p>
<p>The quick brown fox jumps over the lazy dog — The quick brown fox jumps over the lazy dog.
<br>The quick brown fox jumps over the lazy dog — The quick brown fox jumps over the lazy dog.
这是无效的XHTML。但是,我需要将此HTML包含在XML文档中。我尝试使用 XMLFormat()
,以便将<
转换为& lt;
和>
到& gt;
不幸的是,它还会将& mdash;
转换为& amp; mdash;
It is NOT valid XHTML. However, I need to include this HTML in an XML document. I tried using XMLFormat()
in order to convert the <
to <
and the >
to >
, which works great. Unfortunately, it also converts —
to &mdash;
, which is not valid and throws an exception in the CFXML tag.
<cfxml variable="myXML">
<content>#XMLFormat(myHTML)#</content>
</cfxml>
推荐答案
< >解决方案
您有几个选项。很多取决于如何使用这些内容。这将非常有帮助,包括一个所需的输出文档,以及指示这个xml的使用位置。
You have a few options. A lot depends on how this content is going to be used. It would be extremely helpful to include a desired output document, as well as indicate where this xml is being used.
如果你不想搞乱的内容您可以随时使用 CDATA ,例如:
If you don't want to mess with the content of the HTML at all, you could always use CDATA, like this:
<cfxml variable="myXML">
<content><![CDATA[#myHTML#]]></content>
</cfxml>
此外,我知道你说你不想转换剩余的和号,看看这是怎么回事。 HTML内容是您要处理的字符串 - 在这种情况下,应该对其进行转义,以便以后可以将其解除转义,或者您希望将其作为文档的一部分的有效XML。我的意思是,稍后处理< content>
标签的内容时,如果&符号没有转义,您将遇到问题。
Also, I know you say you don't want to convert the remaining ampersands but I just don't see how this is so. Either the HTML content is a string you want to process -- in which case, all of it should be escaped so that it can be unescaped later -- or it's valid XML that you want to be part of the document. I mean, when you process the contents of the <content>
tag later on, you will run into problems if the ampersands aren't escaped.
这篇关于如何在使用ColdFusion函数XMLFormat()时转义HTML字符实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!