XHTML和& (“&"号编码) [英] XHTML and & (Ampersand) encoding

查看:109
本文介绍了XHTML和& (“&"号编码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站是XHTML Transitional兼容的,除了一件事:& URL中的(&"号)按原样写入,而不是&

My website is XHTML Transitional compliant except for one thing: the & (ampersand) in the URL are written as it is, instead of &

也就是说,我页面中的所有网址通常都是这样的:

That is, all the urls in my pages are usually like this:

<a href="http://www.foo.com/page.aspx?x=1&y=2">Foo</a>

但是 XHTML验证器会产生此错误:

无法为一般实体"y"生成系统标识符

cannot generate system identifier for general entity "y"

...,并且它希望这样写网址:

... and it wants the url to be written like this:

<a href="http://www.foo.com/page.aspx?x=1&amp;y=2">Foo</a>

问题在于IE和Firefox无法正确处理URL,而忽略了y参数. 如何使此链接有效并正确验证?

The problem is that IE and Firefox don't handle the URL correctly and ignore the y parameter. How can I make this link work and validate correctly?

在我看来,如果浏览器不能使用严格的编码XHTML URL,就不可能编写XHTML页面.

It seems to me that it is impossible to write XHTML pages if the browsers don't work with strict encoded XHTML URLs.

您想看看实际情况吗?查看这两个链接之间的区别(按原样复制并粘贴):

Do you want to see in action? See the difference between these two links (copy and paste them as they are):

http://stackoverflow.com/search?q=ff&sort=newest

http://stackoverflow.com/search?q=ff&amp;sort=newest

推荐答案

我刚刚尝试过此方法.您尝试做的是正确的.在HTML中,如果要编写链接,则&字符应编码为&amp;如果要让参数值包含与号,则只能将&编码为%26.我刚刚编写了一个包含链接的简单HTML页面:<a href="Default2.aspx?param1=63&amp;param2=hel">Click me</a> 效果很好:default2.aspx收到了预期的参数,并且源通过了验证.

I have just tried this. What you attempted to do is correct. In HTML if you are writing a link the & characters should be encoded as &amp; You would only encode the & as %26 if you wanted a parameter value to contain an ampersand. I just wrote a simple HTML page that contained a link: <a href="Default2.aspx?param1=63&amp;param2=hel">Click me</a> and it worked fine: default2.aspx received the parameters intended and the source passed validation.

在HTML中(而不是在链接中)要求将&编码为&amp;.当浏览器在HTML源代码中看到链接的&amp;时,它将把它解释为&"号,并且链接目标将是预期的.如果将URL粘贴到浏览器地址栏中,则它不会是HTML,也不会尝试解释它可能包含的任何HTML编码.这就是为什么您的示例链接建议我们将其复制/粘贴到浏览器中不起作用以及为什么我们不希望它们起作用的原因.

The encoding of & as &amp; is required in HTML, not in the link. When the browser sees the &amp; in the HTML source for a link it will interpret it as an ampersand and the link target will be as intended. If you paste a URL into your browser address bar it does not expect it to be HTML and does not try to interpret any HTML encoding that it may contain. This is why your example links that you suggest we should copy/paste into a browser don't work and why we wouldn't expect them to work.

如果您发布了更多的实际代码,我们也许可以看到您做错了什么,但是通过在锚标记中使用&amp;,您似乎正在朝着正确的方向前进.

If you post a bit more of your actual code we might be able to see what you have done wrong, but you appear to be heading the right direction by using &amp; in your anchor tags.

这篇关于XHTML和&amp; (“&"号编码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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