Html escapeXml 和 escapeHtml 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3735900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 04:32:04  来源:igfitidea点击:

What is the difference between escapeXml and escapeHtml?

htmlxmljspescaping

提问by eugenn

I would like to escape characters in JSP pages. Which is more suitable, escapeXmlor escapeHtml?

我想转义 JSP 页面中的字符。哪个更合适,escapeXml或者escapeHtml

回答by Rudu

They're designed for different purposes, HTML has lots of entities that XML doesn't. XML only has 5 escapes:

它们是为不同的目的而设计的,HTML 有很多 XML 没有的实体。XML 只有 5 个转义:

&lt; represents "<"
&gt; represents ">"
&amp; represents "&"
&apos; represents '
&quot; represents "

While HTML has loads - think of &nbsp;&copy;etc. These HTML codes aren't valid in XML unless you include a definition in the header. The numeric codes (like &#169;for the copyright symbol) are valid in both.

虽然 HTML 有负载 - 想想&nbsp;&copy;等等。这些 HTML 代码在 XML 中无效,除非您在标题中包含定义。数字代码(如&#169;版权符号)在两者中都有效。

回答by BalusC

There's no such thing as escapeHtmlin JSP. You normally use <c:out escapeXml="true">(it by the way already defaults to true, so you can omit it) or fn:escapeXml()to escape HTML in JSP.

escapeHtml在 JSP 中没有这样的东西。您通常使用<c:out escapeXml="true">(顺便说一下,它已经默认为true,因此您可以省略它)或fn:escapeXml()在 JSP 中转义 HTML。

E.g.

例如

<c:out value="Welcome, ${user.name}" />
<input name="foo" value="${fn:escapeXml(param.foo)}" />

It will escape them as XML entities which works perfectly fine in plain HTML as well. They are only literally called XML entities because HTML entities are invalid in XML.

它会将它们转义为 XML 实体,这在纯 HTML 中也能正常工作。它们只是字面意义上的 XML 实体,因为 HTML 实体在 XML 中是无效的。

See also:

也可以看看:

回答by Justin Niessner

Since you are sending HTML back to the consumer I would go with escapeHtml.

由于您将 HTML 发送回消费者,因此我会使用escapeHtml.

escapeXmlonly supports escaping the five basic XML entities (gt, lt, quot, amp, apos) whereas escapeHtmlsupports escaping all known HTML 4.0 entities.

escapeXml仅支持转义五个基本 XML 实体(gt、lt、quot、amp、apos),而escapeHtml支持转义所有已知的HTML 4.0 实体

回答by Jon Freedman

Assuming you're referring to commons StringEscapeUtils, escapeXmlonly deals with <>"'&while escapeHtmlcovers a richer set of characters.

假设您指的是 commons StringEscapeUtils,escapeXml仅处理<>"'&escapeHtml涵盖更丰富的字符集。