RSS中的URL编码链接断开链接

时间:2011-04-18 15:30:12

标签: xml rss urlencode

为了获得有效的XML RSS提要,我需要对包含'&'的链接进行url编码在查询字符串中。但是,当某人在浏览器中查看RSS源时,链接不会被解码,因此当他们点击链接时,结果页面会被破坏,因为查询字符串参数未被正确读取。

E.g。

http://www.mysite.com?foo1=bar1&foo2=bar2,在浏览器中查看为http://www.mysite.com?foo1=bar1%26foo2=bar2时,参数foo1将被视为bar1%26foo2 = bar2,而不是将水果和饮料分成两个单独的参数。

将我的链接包装在标签中并且不对它们进行编码工作,但是在我的MRSS Feed中,我有一些字段,例如包含标签的字段会破坏XML。

如何正确编码我的链接,以便一切正常?

1 个答案:

答案 0 :(得分:4)

简短回答

使用&amp

答案很长

如果您查看https://stackoverflow.com/feeds/tag/xml,此问题的条目如下所示,并且您在I.E.中查看时,示例链接正常工作。

您的示例链接

http://www.mysite.com?foo1=bar1&foo2=bar2

您的参赛作品

<entry>
        <id>https://stackoverflow.com/questions/5705246/url-encoded-links-in-rss-breaks-links</id>
        <re:rank scheme="http://stackoverflow.com">0</re:rank>
        <title type="text">URL encoded links in RSS breaks links</title>
        <category scheme="https://stackoverflow.com/feeds/tag/xml/tags" term="xml"/><category scheme="https://stackoverflow.com/feeds/tag/xml/tags" term="rss"/><category scheme="https://stackoverflow.com/feeds/tag/xml/tags" term="urlencode"/>
        <author>
            <name>Choy</name>
            <uri>https://stackoverflow.com/users/252529</uri>
        </author>
        <link rel="alternate" href="https://stackoverflow.com/questions/5705246/url-encoded-links-in-rss-breaks-links" />
        <published>2011-04-18T15:30:11Z</published>
        <updated>2011-04-18T15:35:43Z</updated>
        <summary type="html">
            &lt;p&gt;In order to have a valid XML RSS feed, I need to url encode my links which contain &#39;&amp;amp;&#39; in the query string.  However, when a person views the RSS feed in a browser, the links are not decoded, so when they click on the link the resulting page is broken because the query string parameters are not being read correctly.&lt;/p&gt;

&lt;p&gt;E.g.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.mysite.com?foo1=bar1&amp;amp;foo2=bar2&quot; rel=&quot;nofollow&quot;&gt;http://www.mysite.com?foo1=bar1&amp;amp;foo2=bar2&lt;/a&gt;, when viewed in the browser as &lt;a href=&quot;http://www.mysite.com?foo1=bar1%26foo2=bar2&quot; rel=&quot;nofollow&quot;&gt;http://www.mysite.com?foo1=bar1%26foo2=bar2&lt;/a&gt;, the parameter foo1 is being taken as bar1%26foo2=bar2 instead of splitting fruit and drink into two separate parameters.&lt;/p&gt;

&lt;p&gt;Wrapping my links in  tags and not encoding them work, but then in my MRSS feed I have fields such as  where including a  tag breaks the XML.&lt;/p&gt;

&lt;p&gt;How can I go about correctly encoding my links so that everything works?&lt;/p&gt;

        </summary>
    </entry>