我可以在 html 中使用未编码的 & 符号 (&) 吗? [英] Can I use unencoded ampersands (&) in html?

查看:27
本文介绍了我可以在 html 中使用未编码的 & 符号 (&) 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个网站,在那里我必须使用不那么完美的主数据(我想我不是唯一一个:-))

I'm building a website where I have to work with less then perfect masterdata (I guess I'm not the only one :-))

就我而言,我必须将 xml 过滤器呈现为 html(使用 xsl).有时 masterdata 已经在使用 html-enitites(例如法语中的 ;é),所以我必须在那里使用 'disable-output-escaping='yes') 以避免重复编码.

In my case I have to render an xml filte to html (using xsl). Sometimes the masterdata is using html-enitites allready (eg ;é in french words) so there I have to use 'disable-output-escaping='yes') there in order to avoid double encoding.

最简单的解决方案是一起禁用输出转义,所以我从不冒双重编码的风险.

The easiest solution is disable output escaping all together, so I never run the risk of a double encoding.

唯一未对该主数据进行编码的字符是与号.但是当我解析它们原始"时(而不是 & 而不是 & 所有浏览器似乎都可以接受它.

The only characters that misses encoding for this masterdata are the ampersands. But when I parse them 'raw' (so rather & than & all browsers seem to be ok with it.

那么问题是:在 html 中使用未编码的 & 符号的后果是什么?

So the question : what are the consequenses of using not encoded ampersands in html?

推荐答案

AFAIK 裸&符号在 HTML 中是非法的.抛开这些,让我们看看后果:

AFAIK bare ampersands are illegal in HTML. With that out of the way, let's look at the consequences:

  • 您现在依靠浏览器的功能来检测问题并从问题中正常恢复.请注意,为了做到这一点,浏览器必须猜测:显然"是一个&符号后跟一个空格,并且 © 显然是版权符号.但是文本片段 edit&copy 呢?我现在使用的浏览器损坏了它.
  • 如果您使用的是 XHTML,或者内容将被插入到 XML 文档中,结果将是一个硬解析器错误.
  • You are now relying on the browser's capabilities to detect and gracefully recover from the problem. Note that in order to do this, the browser has to guess: is "clearly" an ampersand followed by a space, and © is clearly the copyright symbol. But what about the text fragment edit&copy? The browser I 'm using right now mangles it.
  • If you are using XHTML, or if the content is ever going to be inserted into an XML document, the result will be a hard parser error.

由于手动检测和解释这些情况比替换不属于实体的所有与符号(例如使用正则表达式)更难,因此您应该真正做后者.

Since it's more difficult to detect and account for these cases manually than it is to replace all ampersands that are not part of entities (say with a regex), you should really do the latter.

这篇关于我可以在 html 中使用未编码的 & 符号 (&) 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆