创建 URL 字符串时 XML 中的 & 符号问题
我正在使用一个 XML 提要,该提要的节点之一有一个类似于以下内容的 URL 字符串:
http://aflite.co.uk/track/?aid=13414&mid=32532&dl=http://www.google.com/&aref=chris
我知道与符号会在 XML 中引起很多问题,应该使用 & 而不是裸露的
&
。因此,我将 php 更改为如下所示:
<node><?php echo ('http://aflite.co.uk/track/?aid=13414&mid=32532&dl=http://www.google.com/&aref=chris'); ?></node>
但是,当生成 XML 提要时,该字符串将显示完整的 &
所以实际的 URL 不起作用。如果这是一个非常基本的误解,我深表歉意,但一些指导会很好。
我也尝试过使用 %26
而不是 &
但仍然遇到同样的问题。
I am working with an XML feed that has, as one of it's nodes, a URL string similar to the following:
http://aflite.co.uk/track/?aid=13414&mid=32532&dl=http://www.google.com/&aref=chris
I understand that ampersands cause a lot of problems in XML and should be escaped by using &
instead of a naked &
. I therefore changed the php to read as follows:
<node><?php echo ('http://aflite.co.uk/track/?aid=13414&mid=32532&dl=http://www.google.com/&aref=chris'); ?></node>
However when this generates the XML feed, the string appears with the full &
and so the actual URL does not work. Apologies if this is a very basic misunderstanding but some guidance would be great.
I've also tried using %26
instead of &
but still getting the same problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您要在 XML/HTML 中插入某些内容,则应始终使用 htmlspecialchars 功能。这会将您的字符串转义为正确的 XML 语法。
但你遇到了第二个问题。
您已在第一个网址上添加了第二个网址。
这个需求也转义为 url 语法。
为此,您需要使用 urlencode。
If you are inserting something into XML/HTML you should always use the htmlspecialchars function. this will escape your strings into correct XML syntax.
but you are running into a second problem.
your have added a second url to the first one.
this need also escaped into url syntax.
for this you need to use urlencode.
&
对于在 XML 文档中转义 & 符号是正确的。你给出的例子应该有效。您声明它不起作用,但您没有说明您正在使用什么应用程序,或者它以什么方式不起作用。单击该链接时到底会发生什么?
&
字符串最终会出现在浏览器的 URL 字段中吗?如果是这种情况,听起来您用来查看 XML 的软件有问题。您是否尝试过查看另一个应用程序中的 XML 以查看问题是否一致?回答你问题的最后部分:
%26
肯定不适合你——如果你的 URL 参数需要包含&符号,这就是你应该使用的。例如,在aref=chris
中,如果名称chris
是一个 & 符号(假设用户名是chris&bob
),那么& 符号需要使用%26
进行转义,以便 URL 解析器不会将其视为启动新的 URL 参数。希望有帮助。
&
is correct for escaping ampersands in an XML document. The example you've given should work.You state that it doesn't work, but you haven't stated what application you're using, or in what way it doesn't work. What exactly happens when you click the link? Do the
&
strings end up in the browser's URL field? If that's the case, it sounds like a fault with the software you've viewing the XML with. Have you tried looking at the XML in another application to see if the problem is consistent?To answer the final part of your question:
%26
would definitely not work for you -- this would be what you'd use if your URL parameters needed to contain ampersands. Say for example inaref=chris
, if the namechris
were to an ampersand (lets say the username waschris&bob
), then that ampersand would need to be escaped using%26
so that the URL parser didn't see it as starting a new URL parameter.Hope that helps.